pub fn map_response_with_state<F, S, T>(
    state: S,
    f: F
) -> MapResponseLayer<F, S, T>
Expand description

Create a middleware from an async function that transforms a response, with the given state.

See State for more details about accessing state.

Example

use axum::{
    Router,
    http::StatusCode,
    routing::get,
    response::Response,
    middleware::map_response_with_state,
    extract::State,
};

#[derive(Clone)]
struct AppState { /* ... */ }

async fn my_middleware<B>(
    State(state): State<AppState>,
    // you can add more extractors here but they must
    // all implement `FromRequestParts`
    // `FromRequest` is not allowed
    response: Response<B>,
) -> Response<B> {
    // do something with `state` and `response`...
    response
}

let state = AppState { /* ... */ };

let app = Router::new()
    .route("/", get(|| async { /* ... */ }))
    .route_layer(map_response_with_state(state.clone(), my_middleware))
    .with_state(state);