pub trait FromRequest<S, B, M = ViaRequest>: Sized {
    type Rejection: IntoResponse;

    fn from_request<'life0, 'async_trait>(
        req: Request<B>,
        state: &'life0 S
    ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
; }
Expand description

Types that can be created from requests.

Extractors that implement FromRequest can consume the request body and can thus only be run once for handlers.

If your extractor doesnโ€™t need to consume the request body then you should implement FromRequestParts and not FromRequest.

See axum::extract for more general docs about extraxtors.

What is the B type parameter?

FromRequest is generic over the request body (the B in http::Request<B>). This is to allow FromRequest to be usable with any type of request body. This is necessary because some middleware change the request body, for example to add timeouts.

If youโ€™re writing your own FromRequest that wont be used outside your application, and not using any middleware that changes the request body, you can most likely use axum::body::Body.

If youโ€™re writing a library thatโ€™s intended for others to use, itโ€™s recommended to keep the generic type parameter:

use axum::{
    async_trait,
    extract::FromRequest,
    http::{self, Request},
};

struct MyExtractor;

#[async_trait]
impl<S, B> FromRequest<S, B> for MyExtractor
where
    // these bounds are required by `async_trait`
    B: Send + 'static,
    S: Send + Sync,
{
    type Rejection = http::StatusCode;

    async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
        // ...
    }
}

This ensures your extractor is as flexible as possible.

Required Associated Typesยง

If the extractor fails itโ€™ll use this โ€œrejectionโ€ type. A rejection is a kind of error that can be converted into a response.

Required Methodsยง

Perform the extraction.

Implementations on Foreign Typesยง

Implementorsยง