pub fn map_response<F>(mapper_fn: F) -> MapResMiddleware<F>
Expand description

Creates a middleware from an async function that is used as a mapping function for a ServiceResponse.

Examples

Adds header:

use actix_web::{body::MessageBody, dev::ServiceResponse, http::header};

async fn add_header(
    mut res: ServiceResponse<impl MessageBody>,
) -> actix_web::Result<ServiceResponse<impl MessageBody>> {
    res.headers_mut()
        .insert(header::WARNING, header::HeaderValue::from_static("42"));

    Ok(res)
}

Maps body:

use actix_web::{body::MessageBody, dev::ServiceResponse};

async fn mutate_body_type(
    res: ServiceResponse<impl MessageBody + 'static>,
) -> actix_web::Result<ServiceResponse<impl MessageBody>> {
    Ok(res.map_into_left_body::<()>())
}