pub trait FromRequest<S, B, M = ViaRequest> {
    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::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