Trait actix_web::FromRequest [−][src]
Trait implemented by types that can be extracted from request.
Types that implement this trait can be used with Route
handlers.
Associated Types
type Config: Default + 'static
[src]
Configuration for this extractor.
type Error: Into<Error>
[src]
The associated error which can be returned.
type Future: Future<Output = Result<Self, Self::Error>>
[src]
Future that resolves to a Self.
Required methods
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
Create a Self from request parts asynchronously.
Provided methods
fn extract(req: &HttpRequest) -> Self::Future
[src]
Create a Self from request head asynchronously.
This method is short for T::from_request(req, &mut Payload::None)
.
fn configure<F>(f: F) -> Self::Config where
F: FnOnce(Self::Config) -> Self::Config,
[src]
F: FnOnce(Self::Config) -> Self::Config,
Create and configure config instance.
Implementations on Foreign Types
impl<T: 'static> FromRequest for Option<T> where
T: FromRequest,
T::Future: 'static,
[src]
T: FromRequest,
T::Future: 'static,
Optionally extract a field from the request
If the FromRequest for T fails, return None rather than returning an error response
Example
use actix_web::{web, dev, App, Error, HttpRequest, FromRequest}; use actix_web::error::ErrorBadRequest; use futures_util::future::{ok, err, Ready}; use serde_derive::Deserialize; use rand; #[derive(Debug, Deserialize)] struct Thing { name: String } impl FromRequest for Thing { type Error = Error; type Future = Ready<Result<Self, Self::Error>>; type Config = (); 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!") } } fn main() { let app = App::new().service( web::resource("/users/:first").route( web::post().to(index)) ); }
type Error = Error
type Future = FromRequestOptFuture<T::Future>
type Config = T::Config
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl<T> FromRequest for Result<T, T::Error> where
T: FromRequest + 'static,
T::Error: 'static,
T::Future: 'static,
[src]
T: FromRequest + 'static,
T::Error: 'static,
T::Future: 'static,
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
use actix_web::{web, dev, App, Result, Error, HttpRequest, FromRequest}; use actix_web::error::ErrorBadRequest; use futures_util::future::{ok, err, Ready}; use serde_derive::Deserialize; use rand; #[derive(Debug, Deserialize)] struct Thing { name: String } impl FromRequest for Thing { type Error = Error; type Future = Ready<Result<Thing, Error>>; type Config = (); 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) } } fn main() { let app = App::new().service( web::resource("/users/:first").route(web::post().to(index)) ); }
type Error = Error
type Future = FromRequestResFuture<T::Future>
type Config = T::Config
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl FromRequest for String
[src]
Extract text information from a request's body.
Text extractor automatically decode body according to the request's charset.
PayloadConfig allows 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 Config = PayloadConfig
type Error = Error
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
Implementors
impl FromRequest for Bytes
[src]
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) }
type Config = PayloadConfig
type Error = Error
type Future = Either<ErrInto<HttpMessageBody, Error>, Ready<Result<Bytes, Error>>>
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl FromRequest for HttpRequest
[src]
It is possible to get HttpRequest
as an extractor handler parameter
Example
use actix_web::{web, App, HttpRequest}; use serde_derive::Deserialize; /// extract `Thing` from request async fn index(req: HttpRequest) -> String { format!("Got thing: {:?}", req) } fn main() { let app = App::new().service( web::resource("/users/{first}").route( web::get().to(index)) ); }
type Config = ()
type Error = Error
type Future = Ready<Result<Self, Error>>
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
[src]
impl FromRequest for Payload
[src]
See here for example of usage as an extractor.
type Config = PayloadConfig
type Error = Error
type Future = Ready<Result<Payload, Error>>
fn from_request(_: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl<L, R> FromRequest for Either<L, R> where
L: FromRequest + 'static,
R: FromRequest + 'static,
[src]
L: FromRequest + 'static,
R: FromRequest + 'static,
See here for example of usage as an extractor.
type Error = EitherExtractError<L::Error, R::Error>
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>
type Config = ()
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl<T> FromRequest for Form<T> where
T: DeserializeOwned + 'static,
[src]
T: DeserializeOwned + 'static,
See here for example of usage as an extractor.
type Config = FormConfig
type Error = Error
type Future = LocalBoxFuture<'static, Result<Self, Error>>
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl<T> FromRequest for Json<T> where
T: DeserializeOwned + 'static,
[src]
T: DeserializeOwned + 'static,
See here for example of usage as an extractor.
type Error = Error
type Future = JsonExtractFut<T>
type Config = JsonConfig
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
[src]
impl<T> FromRequest for Path<T> where
T: DeserializeOwned,
[src]
T: DeserializeOwned,
See here for example of usage as an extractor.
type Error = Error
type Future = Ready<Result<Self, Error>>
type Config = PathConfig
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
[src]
impl<T> FromRequest for Query<T> where
T: DeserializeOwned,
[src]
T: DeserializeOwned,
See here for example of usage as an extractor.