# actix_web::FromRequest - Rust
API documentation for the Rust `FromRequest` trait in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, FromRequest

# [−] [src] Trait actix_web::FromRequest

    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 { ... }
    }

Trait implemented by types that can be extracted from request.

Types that implement this trait can be used with `Route::with()` method.

## Associated Types

### `type Config: Default`

Configuration for conversion process

### `type Result: Into<AsyncResult<Self>>`

Future that resolves to a Self

## Required Methods

### `fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

Convert request to a Self

## Provided Methods

### `fn extract(req: &HttpRequest<S>) -> Self::Result`

Convert request to a Self

This method uses default extractor configuration

## Implementations on Foreign Types

###

`impl<S: 'static> FromRequest<S> for Bytes`

[src]

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<Future<Item = Self, Error = Error>>, Error>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S: 'static> FromRequest<S> for String`

[src]

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<Future<Item = String, Error = Error>>, Error>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<T: 'static, S: 'static> FromRequest<S> for Option<T> where T: FromRequest<S>,`

[src]

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::Config`

#### `type Result = Box<Future<Item = Option<T>, Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<T: 'static, S: 'static> FromRequest<S> for Result<T, Error> where T: FromRequest<S>,`

[src]

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::Config`

#### `type Result = Box<Future<Item = Result<T, Error>, Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S> FromRequest<S> for ()`

[src]

#### `type Config = ()`

#### `type Result = Self`

####

`fn from_request(_req: &HttpRequest<S>, _cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S, A: FromRequest<S> + 'static> FromRequest<S> for (A,) where S: 'static,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config,)`

#### `type Result = Box<Future<Item = (A,), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static> FromRequest<S> for (A, B) where S: 'static,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config)`

#### `type Result = Box<Future<Item = (A, B), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S, A: FromRequest<S> + 'static, B: FromRequest<S> + 'static, C: FromRequest<S> + 'static> FromRequest<S> for (A, B, C) where S: 'static,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config)`

#### `type Result = Box<Future<Item = (A, B, C), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`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,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config, D::Config)`

#### `type Result = Box<Future<Item = (A, B, C, D), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`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,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config, D::Config, E::Config)`

#### `type Result = Box<Future<Item = (A, B, C, D, E), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`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,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config, D::Config, E::Config, F::Config)`

#### `type Result = Box<Future<Item = (A, B, C, D, E, F), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`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,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config, D::Config, E::Config, F::Config, G::Config)`

#### `type Result = Box<Future<Item = (A, B, C, D, E, F, G), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`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,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config, D::Config, E::Config, F::Config, G::Config, H::Config)`

#### `type Result = Box<Future<Item = (A, B, C, D, E, F, G, H), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`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,`

[src]

FromRequest implementation for tuple

#### `type Config = (A::Config, B::Config, C::Config, D::Config, E::Config, F::Config, G::Config, H::Config, I::Config)`

#### `type Result = Box<Future<Item = (A, B, C, D, E, F, G, H, I), Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

## Implementors

###

`impl<S> FromRequest<S> for Session`

[src]

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!")
    }

#### `type Config = ()`

#### `type Result = Session`

####

`fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S> FromRequest<S> for HttpRequest<S>`

[src]

#### `type Config = ()`

#### `type Result = Self`

####

`fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<S> FromRequest<S> for State<S>`

[src]

#### `type Config = ()`

#### `type Result = State<S>`

####

`fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<T, S> FromRequest<S> for Form<T> where T: DeserializeOwned + 'static, S: 'static,`

[src]

#### `type Config = FormConfig<S>`

#### `type Result = Box<Future<Item = Self, Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<T, S> FromRequest<S> for Json<T> where T: DeserializeOwned + 'static, S: 'static,`

[src]

#### `type Config = JsonConfig<S>`

#### `type Result = Box<Future<Item = Self, Error = Error>>`

####

`fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<T, S> FromRequest<S> for Path<T> where T: DeserializeOwned,`

[src]

#### `type Config = ()`

#### `type Result = Result<Self, Error>`

####

`fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

###

`impl<T, S> FromRequest<S> for Query<T> where T: DeserializeOwned,`

[src]

#### `type Config = ()`

#### `type Result = Result<Self, Error>`

####

`fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result`

[src]

####

`fn extract(req: &HttpRequest<S>) -> Self::Result`

[src]

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)