Trait ajars_actix_web::actix_web::FromRequest
source · [−]pub trait FromRequest {
type Error: Into<Error>;
type Future: Future
where
<Self::Future as Future>::Output == Result<Self, Self::Error>;
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> Self::Future;
fn extract(req: &HttpRequest) -> Self::Future { ... }
}
Expand description
A type that implements FromRequest
is called an extractor and can extract data from
the request. Some types that implement this trait are: Json
, Header
, and Path
.
Configuration
An extractor can be customized by injecting the corresponding configuration with one of:
Here are some built-in extractors and their corresponding configuration. Please refer to the respective documentation for details.
Extractor | Configuration |
---|---|
Header | None |
Path | PathConfig |
Json | JsonConfig |
Form | FormConfig |
Query | QueryConfig |
Bytes | PayloadConfig |
String | PayloadConfig |
Payload | PayloadConfig |
Implementing An Extractor
To reduce duplicate code in handlers where extracting certain parts of a request has a common
structure, you can implement FromRequest
for your own types.
Note that the request payload can only be consumed by one extractor.
Associated Types
Required methods
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> Self::Future
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> Self::Future
Create a Self from request parts asynchronously.
Provided methods
fn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Create a Self from request head asynchronously.
This method is short for T::from_request(req, &mut Payload::None)
.
Implementations on Foreign Types
sourceimpl<A, B, C, D, E, F, G, H, I, J, K, L> FromRequest for (A, B, C, D, E, F, G, H, I, J, K, L) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
J: 'static + FromRequest,
K: 'static + FromRequest,
L: 'static + FromRequest,
impl<A, B, C, D, E, F, G, H, I, J, K, L> FromRequest for (A, B, C, D, E, F, G, H, I, J, K, L) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
J: 'static + FromRequest,
K: 'static + FromRequest,
L: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest12<A, B, C, D, E, F, G, H, I, J, K, L>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F, G, H, I, J, K, L) as FromRequest>::Future
sourceimpl<A, B, C, D, E, F, G, H, I, J, K> FromRequest for (A, B, C, D, E, F, G, H, I, J, K) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
J: 'static + FromRequest,
K: 'static + FromRequest,
impl<A, B, C, D, E, F, G, H, I, J, K> FromRequest for (A, B, C, D, E, F, G, H, I, J, K) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
J: 'static + FromRequest,
K: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest11<A, B, C, D, E, F, G, H, I, J, K>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F, G, H, I, J, K) as FromRequest>::Future
sourceimpl<T> FromRequest for Option<T> where
T: FromRequest,
impl<T> FromRequest for Option<T> where
T: FromRequest,
Optionally extract a field from the request
If the FromRequest for T fails, return None rather than returning an error response
Examples
use actix_web::{web, dev, App, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorBadRequest;
use futures_util::future::{ok, err, Ready};
use serde::Deserialize;
use rand;
#[derive(Debug, Deserialize)]
struct Thing {
name: String
}
impl FromRequest for Thing {
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
if rand::random() {
ok(Thing { name: "thingy".into() })
} else {
err(ErrorBadRequest("no luck"))
}
}
}
/// extract `Thing` from request
async fn index(supplied_thing: Option<Thing>) -> String {
match supplied_thing {
// Puns not intended
Some(thing) => format!("Got something: {:?}", thing),
None => format!("No thing!")
}
}
let app = App::new().service(
web::resource("/users/:first").route(
web::post().to(index))
);
type Error = Infallible
type Future = FromRequestOptFuture<<T as FromRequest>::Future>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Option<T> as FromRequest>::Future
sourceimpl<A, B, C, D, E, F, G, H> FromRequest for (A, B, C, D, E, F, G, H) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
impl<A, B, C, D, E, F, G, H> FromRequest for (A, B, C, D, E, F, G, H) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest8<A, B, C, D, E, F, G, H>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F, G, H) as FromRequest>::Future
sourceimpl<T, E> FromRequest for Result<T, E> where
T: FromRequest,
<T as FromRequest>::Error: Into<E>,
impl<T, E> FromRequest for Result<T, E> where
T: FromRequest,
<T as FromRequest>::Error: Into<E>,
Optionally extract a field from the request or extract the Error if unsuccessful
If the FromRequest
for T fails, inject Err into handler rather than returning an error response
Examples
use actix_web::{web, dev, App, Result, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorBadRequest;
use futures_util::future::{ok, err, Ready};
use serde::Deserialize;
use rand;
#[derive(Debug, Deserialize)]
struct Thing {
name: String
}
impl FromRequest for Thing {
type Error = Error;
type Future = Ready<Result<Thing, Error>>;
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
if rand::random() {
ok(Thing { name: "thingy".into() })
} else {
err(ErrorBadRequest("no luck"))
}
}
}
/// extract `Thing` from request
async fn index(supplied_thing: Result<Thing>) -> String {
match supplied_thing {
Ok(thing) => format!("Got thing: {:?}", thing),
Err(e) => format!("Error extracting thing: {}", e)
}
}
let app = App::new().service(
web::resource("/users/:first").route(web::post().to(index))
);
type Error = Infallible
type Future = FromRequestResFuture<<T as FromRequest>::Future, E>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Result<T, E> as FromRequest>::Future
sourceimpl<A, B, C> FromRequest for (A, B, C) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
impl<A, B, C> FromRequest for (A, B, C) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest3<A, B, C>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C) as FromRequest>::Future
sourceimpl<A, B, C, D, E, F, G, H, I> FromRequest for (A, B, C, D, E, F, G, H, I) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
impl<A, B, C, D, E, F, G, H, I> FromRequest for (A, B, C, D, E, F, G, H, I) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest9<A, B, C, D, E, F, G, H, I>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F, G, H, I) as FromRequest>::Future
sourceimpl FromRequest for String
impl FromRequest for String
Extract text information from a request’s body.
Text extractor automatically decode body according to the request’s charset.
Use PayloadConfig
to configure extraction process.
Examples
use actix_web::{post, web, FromRequest};
// extract text data from request
#[post("/")]
async fn index(text: String) -> String {
format!("Body {}!", text)
}
type Error = Error
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <String as FromRequest>::Future
sourceimpl<A, B, C, D, E, F> FromRequest for (A, B, C, D, E, F) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
impl<A, B, C, D, E, F> FromRequest for (A, B, C, D, E, F) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest6<A, B, C, D, E, F>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F) as FromRequest>::Future
sourceimpl<A, B, C, D, E, F, G> FromRequest for (A, B, C, D, E, F, G) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
impl<A, B, C, D, E, F, G> FromRequest for (A, B, C, D, E, F, G) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest7<A, B, C, D, E, F, G>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F, G) as FromRequest>::Future
sourceimpl<A, B, C, D> FromRequest for (A, B, C, D) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
impl<A, B, C, D> FromRequest for (A, B, C, D) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest4<A, B, C, D>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D) as FromRequest>::Future
sourceimpl<A> FromRequest for (A,) where
A: 'static + FromRequest,
impl<A> FromRequest for (A,) where
A: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest1<A>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A,) as FromRequest>::Future
sourceimpl<A, B, C, D, E, F, G, H, I, J> FromRequest for (A, B, C, D, E, F, G, H, I, J) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
J: 'static + FromRequest,
impl<A, B, C, D, E, F, G, H, I, J> FromRequest for (A, B, C, D, E, F, G, H, I, J) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
F: 'static + FromRequest,
G: 'static + FromRequest,
H: 'static + FromRequest,
I: 'static + FromRequest,
J: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest10<A, B, C, D, E, F, G, H, I, J>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E, F, G, H, I, J) as FromRequest>::Future
sourceimpl<A, B, C, D, E> FromRequest for (A, B, C, D, E) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
impl<A, B, C, D, E> FromRequest for (A, B, C, D, E) where
A: 'static + FromRequest,
B: 'static + FromRequest,
C: 'static + FromRequest,
D: 'static + FromRequest,
E: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest5<A, B, C, D, E>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B, C, D, E) as FromRequest>::Future
sourceimpl<A, B> FromRequest for (A, B) where
A: 'static + FromRequest,
B: 'static + FromRequest,
impl<A, B> FromRequest for (A, B) where
A: 'static + FromRequest,
B: 'static + FromRequest,
FromRequest implementation for tuple
type Error = Error
type Future = TupleFromRequest2<A, B>
fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <(A, B) as FromRequest>::Future
sourceimpl FromRequest for ()
impl FromRequest for ()
type Error = Infallible
type Future = Ready<Result<(), <() as FromRequest>::Error>>
fn from_request(
&HttpRequest,
&mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <() as FromRequest>::Future
Implementors
sourceimpl FromRequest for ConnectionInfo
impl FromRequest for ConnectionInfo
type Error = Infallible
type Future = Ready<Result<ConnectionInfo, <ConnectionInfo as FromRequest>::Error>>
sourceimpl FromRequest for PeerAddr
impl FromRequest for PeerAddr
sourceimpl FromRequest for Method
impl FromRequest for Method
Extract the request’s method.
Examples
use actix_web::{http::Method, web, App, Responder};
async fn handler(method: Method) -> impl Responder {
format!("Request method: {}", method)
}
let app = App::new().default_service(web::to(handler));
type Error = Infallible
type Future = Ready<Result<Method, <Method as FromRequest>::Error>>
sourceimpl FromRequest for Uri
impl FromRequest for Uri
Extract the request’s URI.
Examples
use actix_web::{http::Uri, web, App, Responder};
async fn handler(uri: Uri) -> impl Responder {
format!("Requested path: {}", uri.path())
}
let app = App::new().default_service(web::to(handler));
type Error = Infallible
type Future = Ready<Result<Uri, <Uri as FromRequest>::Error>>
sourceimpl FromRequest for HttpRequest
impl FromRequest for HttpRequest
It is possible to get HttpRequest
as an extractor handler parameter
Examples
use actix_web::{web, App, HttpRequest};
use serde::Deserialize;
/// extract `Thing` from request
async fn index(req: HttpRequest) -> String {
format!("Got thing: {:?}", req)
}
let app = App::new().service(
web::resource("/users/{first}").route(
web::get().to(index))
);
sourceimpl FromRequest for Bytes
impl FromRequest for Bytes
Extract binary data from a request’s payload.
Collects request payload stream into a Bytes instance.
Use PayloadConfig
to configure extraction process.
Examples
use actix_web::{post, web};
/// extract binary data from request
#[post("/")]
async fn index(body: web::Bytes) -> String {
format!("Body {:?}!", body)
}
sourceimpl FromRequest for Payload
impl FromRequest for Payload
See here for example of usage as an extractor.
sourceimpl<L, R> FromRequest for Either<L, R> where
L: 'static + FromRequest,
R: 'static + FromRequest,
impl<L, R> FromRequest for Either<L, R> where
L: 'static + FromRequest,
R: 'static + FromRequest,
See here for example of usage as an extractor.
type Error = EitherExtractError<<L as FromRequest>::Error, <R as FromRequest>::Error>
type Future = EitherExtractFut<L, R>
sourceimpl<T> FromRequest for Data<T> where
T: 'static + ?Sized,
impl<T> FromRequest for Data<T> where
T: 'static + ?Sized,
sourceimpl<T> FromRequest for Form<T> where
T: 'static + DeserializeOwned,
impl<T> FromRequest for Form<T> where
T: 'static + DeserializeOwned,
See here for example of usage as an extractor.
sourceimpl<T> FromRequest for Header<T> where
T: Header,
impl<T> FromRequest for Header<T> where
T: Header,
type Error = ParseError
type Future = Ready<Result<Header<T>, <Header<T> as FromRequest>::Error>>
sourceimpl<T> FromRequest for Json<T> where
T: DeserializeOwned,
impl<T> FromRequest for Json<T> where
T: DeserializeOwned,
See here for example of usage as an extractor.
sourceimpl<T> FromRequest for Path<T> where
T: DeserializeOwned,
impl<T> FromRequest for Path<T> where
T: DeserializeOwned,
See here for example of usage as an extractor.
sourceimpl<T> FromRequest for Query<T> where
T: DeserializeOwned,
impl<T> FromRequest for Query<T> where
T: DeserializeOwned,
See here for example of usage as an extractor.