Trait actix_web::FromRequest
source · pub trait FromRequest<S>: Sized {
type Config: Default;
type Result: Into<AsyncResult<Self>>;
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result;
fn extract(req: &HttpRequest<S>) -> Self::Result { ... }
}
Expand description
Trait implemented by types that can be extracted from request.
Types that implement this trait can be used with Route::with()
method.
Required Associated Types
sourcetype Result: Into<AsyncResult<Self>>
type Result: Into<AsyncResult<Self>>
Future that resolves to a Self
Required Methods
sourcefn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
Convert request to a Self
Provided Methods
sourcefn extract(req: &HttpRequest<S>) -> Self::Result
fn extract(req: &HttpRequest<S>) -> Self::Result
Convert request to a Self
This method uses default extractor configuration
Implementations on Foreign Types
sourceimpl<S: 'static> FromRequest<S> for Bytes
impl<S: 'static> FromRequest<S> for Bytes
Request payload extractor.
Loads request’s payload and construct Bytes instance.
PayloadConfig allows to configure extraction process.
Example
extern crate bytes;
use actix_web::{http, App, Result};
/// extract text data from request
fn index(body: bytes::Bytes) -> Result<String> {
Ok(format!("Body {:?}!", body))
}
fn main() {
let app = App::new()
.resource("/index.html", |r| r.method(http::Method::GET).with(index));
}
type Config = PayloadConfig
type Result = Result<Box<dyn Future<Error = Error, Item = Bytes> + 'static, Global>, Error>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S: 'static> FromRequest<S> for String
impl<S: 'static> FromRequest<S> for String
Extract text information from the request’s body.
Text extractor automatically decode body according to the request’s charset.
PayloadConfig allows to configure extraction process.
Example
use actix_web::{http, App, Result};
/// extract text data from request
fn index(body: String) -> Result<String> {
Ok(format!("Body {}!", body))
}
fn main() {
let app = App::new().resource("/index.html", |r| {
r.method(http::Method::GET)
.with_config(index, |cfg| { // <- register handler with extractor params
cfg.0.limit(4096); // <- limit size of the payload
})
});
}
type Config = PayloadConfig
type Result = Result<Box<dyn Future<Error = Error, Item = String> + 'static, Global>, Error>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<T: 'static, S: 'static> FromRequest<S> for Option<T>where
T: FromRequest<S>,
impl<T: 'static, S: 'static> FromRequest<S> for Option<T>where
T: FromRequest<S>,
Optionally extract a field from the request
If the FromRequest for T fails, return None rather than returning an error response
Example
extern crate rand;
#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Result, HttpRequest, Error, FromRequest};
use actix_web::error::ErrorBadRequest;
#[derive(Debug, Deserialize)]
struct Thing { name: String }
impl<S> FromRequest<S> for Thing {
type Config = ();
type Result = Result<Thing, Error>;
#[inline]
fn from_request(req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result {
if rand::random() {
Ok(Thing { name: "thingy".into() })
} else {
Err(ErrorBadRequest("no luck"))
}
}
}
/// extract text data from request
fn index(supplied_thing: Option<Thing>) -> Result<String> {
match supplied_thing {
// Puns not intended
Some(thing) => Ok(format!("Got something: {:?}", thing)),
None => Ok(format!("No thing!"))
}
}
fn main() {
let app = App::new().resource("/users/:first", |r| {
r.method(http::Method::POST).with(index)
});
}
type Config = <T as FromRequest<S>>::Config
type Result = Box<dyn Future<Error = Error, Item = Option<T>> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<T: 'static, S: 'static> FromRequest<S> for Result<T, Error>where
T: FromRequest<S>,
impl<T: 'static, S: 'static> FromRequest<S> for Result<T, Error>where
T: FromRequest<S>,
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
Example
extern crate rand;
#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Result, HttpRequest, Error, FromRequest};
use actix_web::error::ErrorBadRequest;
#[derive(Debug, Deserialize)]
struct Thing { name: String }
impl<S> FromRequest<S> for Thing {
type Config = ();
type Result = Result<Thing, Error>;
#[inline]
fn from_request(req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result {
if rand::random() {
Ok(Thing { name: "thingy".into() })
} else {
Err(ErrorBadRequest("no luck"))
}
}
}
/// extract text data from request
fn index(supplied_thing: Result<Thing>) -> Result<String> {
match supplied_thing {
Ok(thing) => Ok(format!("Got thing: {:?}", thing)),
Err(e) => Ok(format!("Error extracting thing: {}", e))
}
}
fn main() {
let app = App::new().resource("/users/:first", |r| {
r.method(http::Method::POST).with(index)
});
}
type Config = <T as FromRequest<S>>::Config
type Result = Box<dyn Future<Error = Error, Item = Result<T, Error>> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S> FromRequest<S> for ()
impl<S> FromRequest<S> for ()
type Config = ()
type Result = ()
fn from_request(_req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static> FromRequest<S> for (A,)where
S: 'static,
impl<S, A: FromRequest<S> + 'static> FromRequest<S> for (A,)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config,)
type Result = Box<dyn Future<Error = Error, Item = (A,)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static> FromRequest<S> for (A, B)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static> FromRequest<S> for (A, B)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static> FromRequest<S> for (A, B, C)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static> FromRequest<S> for (A, B, C)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config, <D as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C, D)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config, <D as FromRequest<S>>::Config, <E as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C, D, E)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config, <D as FromRequest<S>>::Config, <E as FromRequest<S>>::Config, <F as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C, D, E, F)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static, G: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F, G)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static, G: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F, G)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config, <D as FromRequest<S>>::Config, <E as FromRequest<S>>::Config, <F as FromRequest<S>>::Config, <G as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C, D, E, F, G)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static, G: FromRequest<S> + 'static, H: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F, G, H)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static, G: FromRequest<S> + 'static, H: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F, G, H)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config, <D as FromRequest<S>>::Config, <E as FromRequest<S>>::Config, <F as FromRequest<S>>::Config, <G as FromRequest<S>>::Config, <H as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C, D, E, F, G, H)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
sourceimpl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static, G: FromRequest<S> + 'static, H: FromRequest<S> + 'static, I: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F, G, H, I)where
S: 'static,
impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static, D: FromRequest<S> + 'static, E: FromRequest<S> + 'static, F: FromRequest<S> + 'static, G: FromRequest<S> + 'static, H: FromRequest<S> + 'static, I: FromRequest<S> + 'static> FromRequest<S> for (A, B, C, D, E, F, G, H, I)where
S: 'static,
FromRequest implementation for tuple
type Config = (<A as FromRequest<S>>::Config, <B as FromRequest<S>>::Config, <C as FromRequest<S>>::Config, <D as FromRequest<S>>::Config, <E as FromRequest<S>>::Config, <F as FromRequest<S>>::Config, <G as FromRequest<S>>::Config, <H as FromRequest<S>>::Config, <I as FromRequest<S>>::Config)
type Result = Box<dyn Future<Error = Error, Item = (A, B, C, D, E, F, G, H, I)> + 'static, Global>
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result
Implementors
sourceimpl<A: 'static, B: 'static, S: 'static> FromRequest<S> for Either<A, B>where
A: FromRequest<S>,
B: FromRequest<S>,
impl<A: 'static, B: 'static, S: 'static> FromRequest<S> for Either<A, B>where
A: FromRequest<S>,
B: FromRequest<S>,
Extract either one of two fields from the request.
If both or none of the fields can be extracted, the default behaviour is to prefer the first
successful, last that failed. The behaviour can be changed by setting the appropriate
EitherCollisionStrategy
.
CAVEAT: Most of the time both extractors will be run. Make sure that the extractors you specify
can be run one after another (or in parallel). This will always fail for extractors that modify
the request state (such as the Form
extractors that read in the body stream).
So Either<Form, Form> will not work correctly - it will only succeed if it matches the first
option, but will always fail to match the second (since the body stream will be at the end, and
appear to be empty).
Example
extern crate rand;
#[macro_use] extern crate serde_derive;
use actix_web::{http, App, Result, HttpRequest, Error, FromRequest};
use actix_web::error::ErrorBadRequest;
use actix_web::Either;
#[derive(Debug, Deserialize)]
struct Thing { name: String }
#[derive(Debug, Deserialize)]
struct OtherThing { id: String }
impl<S> FromRequest<S> for Thing {
type Config = ();
type Result = Result<Thing, Error>;
#[inline]
fn from_request(req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result {
if rand::random() {
Ok(Thing { name: "thingy".into() })
} else {
Err(ErrorBadRequest("no luck"))
}
}
}
impl<S> FromRequest<S> for OtherThing {
type Config = ();
type Result = Result<OtherThing, Error>;
#[inline]
fn from_request(req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result {
if rand::random() {
Ok(OtherThing { id: "otherthingy".into() })
} else {
Err(ErrorBadRequest("no luck"))
}
}
}
/// extract text data from request
fn index(supplied_thing: Either<Thing, OtherThing>) -> Result<String> {
match supplied_thing {
Either::A(thing) => Ok(format!("Got something: {:?}", thing)),
Either::B(other_thing) => Ok(format!("Got anotherthing: {:?}", other_thing))
}
}
fn main() {
let app = App::new().resource("/users/:first", |r| {
r.method(http::Method::POST).with(index)
});
}
type Config = EitherConfig<A, B, S>
type Result = AsyncResult<Either<A, B>, Error>
sourceimpl<S> FromRequest<S> for Session
impl<S> FromRequest<S> for Session
Extractor implementation for Session type.
use actix_web::middleware::session::Session;
fn index(session: Session) -> Result<&'static str> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.set("counter", count + 1)?;
} else {
session.set("counter", 1)?;
}
Ok("Welcome!")
}