Derive Macro axum_macros::FromRequestParts

source ·
#[derive(FromRequestParts)]
{
    // Attributes available to this derive:
    #[from_request]
}
Expand description

Derive an implementation of FromRequestParts.

This works similarly to #[derive(FromRequest)] except it uses FromRequestParts. All the same options are supported.

§Example

use axum_macros::FromRequestParts;
use axum::{
    extract::Query,
};
use axum_extra::{
    TypedHeader,
    headers::ContentType,
};
use std::collections::HashMap;

#[derive(FromRequestParts)]
struct MyExtractor {
    #[from_request(via(Query))]
    query_params: HashMap<String, String>,
    content_type: TypedHeader<ContentType>,
}

async fn handler(extractor: MyExtractor) {}

§Cannot extract the body

FromRequestParts cannot extract the request body:

use axum_macros::FromRequestParts;

#[derive(FromRequestParts)]
struct MyExtractor {
    body: String,
}

Use #[derive(FromRequest)] for that.