pub trait FromRequest<B>: Sized {
    type Rejection: IntoResponse;

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

Types that can be created from requests.

See axum::extract for more details.

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, RequestParts},
};

struct MyExtractor;

#[async_trait]
impl<B> FromRequest<B> for MyExtractor
where
    B: Send, // required by `async_trait`
{
    type Rejection = http::StatusCode;

    async fn from_request(req: &mut RequestParts<B>) -> 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

Clone the headers from the request.

Prefer using TypedHeader to extract only the headers you need.

Implementors