Skip to main content

FromRequest

Trait FromRequest 

Source
pub trait FromRequest: Sized + Send {
    type Error: IntoResponse;

    // Required method
    fn from_request(
        parts: &Parts,
        body: Bytes,
    ) -> impl Future<Output = Result<Self, Self::Error>> + Send;
}
Expand description

Extract a value by consuming the request body.

At most one FromRequest extractor can appear per handler (as the last argument), since it consumes the body.

Required Associated Types§

Source

type Error: IntoResponse

The error type returned when extraction fails.

Required Methods§

Source

fn from_request( parts: &Parts, body: Bytes, ) -> impl Future<Output = Result<Self, Self::Error>> + Send

Extract this type from the request parts and pre-collected body bytes.

This is async for interface consistency, though body bytes are already collected by the router before dispatch.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromRequest for ()

Unit extractor — always succeeds, ignoring the body.

Source§

type Error = (StatusCode, String)

Source§

async fn from_request(_parts: &Parts, _body: Bytes) -> Result<Self, Self::Error>

Source§

impl FromRequest for String

Source§

type Error = (StatusCode, String)

Source§

async fn from_request(_parts: &Parts, body: Bytes) -> Result<Self, Self::Error>

Source§

impl FromRequest for Bytes

Source§

type Error = (StatusCode, String)

Source§

async fn from_request(_parts: &Parts, body: Bytes) -> Result<Self, Self::Error>

Implementors§

Source§

impl<T: DeserializeOwned + Send> FromRequest for Json<T>

JSON request body extractor.

Parses the request body as JSON. Requires Content-Type: application/json.

§Example

async fn create_user(Json(body): Json<CreateUser>) -> Json<User> {
    // body: CreateUser, deserialized from JSON
}