Trait ajars_actix_web::actix_web::FromRequest [−][src]
pub trait FromRequest {
type Config: 'static + Default;
type Error: Into<Error>;
type Future: Future;
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 { ... }
fn configure<F>(f: F) -> Self::Config
where
F: FnOnce(Self::Config) -> Self::Config,
{ ... }
}
Expand description
Trait implemented by types that can be extracted from request.
Types that implement this trait can be used with Route
handlers.
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
impl<T> FromRequest for Option<T> where
T: 'static + FromRequest,
<T as FromRequest>::Future: 'static,
impl<T> FromRequest for Option<T> where
T: 'static + FromRequest,
<T as FromRequest>::Future: 'static,
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>>;
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 Future = FromRequestOptFuture<<T as FromRequest>::Future>
type Config = <T as FromRequest>::Config
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Option<T> as FromRequest>::Future
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
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <String as FromRequest>::Future
impl<T> FromRequest for Result<T, <T as FromRequest>::Error> where
T: FromRequest + 'static,
<T as FromRequest>::Error: 'static,
<T as FromRequest>::Future: 'static,
impl<T> FromRequest for Result<T, <T as FromRequest>::Error> where
T: FromRequest + 'static,
<T as FromRequest>::Error: 'static,
<T as FromRequest>::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
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>>;
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))
);
}