Skip to main content

IntoResponseParts

Trait IntoResponseParts 

pub trait IntoResponseParts {
    type Error: IntoResponse;

    // Required method
    fn into_response_parts(
        self,
        res: ResponseParts,
    ) -> Result<ResponseParts, Self::Error>;
}
Expand description

Trait for adding headers and extensions to a response.

ยงExample

use axum::{
    response::{ResponseParts, IntoResponse, IntoResponseParts, Response},
    http::{StatusCode, header::{HeaderName, HeaderValue}},
};

// Hypothetical helper type for setting a single header
struct SetHeader<'a>(&'a str, &'a str);

impl<'a> IntoResponseParts for SetHeader<'a> {
    type Error = (StatusCode, String);

    fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
        match (self.0.parse::<HeaderName>(), self.1.parse::<HeaderValue>()) {
            (Ok(name), Ok(value)) => {
                res.headers_mut().insert(name, value);
            },
            (Err(_), _) => {
                return Err((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Invalid header name {}", self.0),
                ));
            },
            (_, Err(_)) => {
                return Err((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Invalid header value {}", self.1),
                ));
            },
        }

        Ok(res)
    }
}

// It's also recommended to implement `IntoResponse` so `SetHeader` can be used on its own as
// the response
impl<'a> IntoResponse for SetHeader<'a> {
    fn into_response(self) -> Response {
        // This gives an empty response with the header
        (self, ()).into_response()
    }
}

// We can now return `SetHeader` in responses
//
// Note that returning `impl IntoResponse` might be easier if the response has many parts to
// it. The return type is written out here for clarity.
async fn handler() -> (SetHeader<'static>, SetHeader<'static>, &'static str) {
    (
        SetHeader("server", "axum"),
        SetHeader("x-foo", "custom"),
        "body",
    )
}

// Or on its own as the whole response
async fn other_handler() -> SetHeader<'static> {
    SetHeader("x-foo", "custom")
}

Required Associated Typesยง

type Error: IntoResponse

The type returned in the event of an error.

This can be used to fallibly convert types into headers or extensions.

Required Methodsยง

fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::Error>

Set parts of the response

Dyn Compatibilityยง

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Typesยง

ยง

impl IntoResponseParts for ()

ยง

impl IntoResponseParts for Extensions

ยง

impl IntoResponseParts for HeaderMap

ยง

impl<K, V, const N: usize> IntoResponseParts for [(K, V); N]
where K: TryInto<HeaderName>, <K as TryInto<HeaderName>>::Error: Display, V: TryInto<HeaderValue>, <V as TryInto<HeaderValue>>::Error: Display,

ยง

type Error = TryIntoHeaderError<<K as TryInto<HeaderName>>::Error, <V as TryInto<HeaderValue>>::Error>

ยง

fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, <[(K, V); N] as IntoResponseParts>::Error>

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

ยง

impl<T1, T2, T3, T4, T5, T6, T7, T8> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7, T8)

ยง

impl<T1, T2, T3, T4, T5, T6, T7> IntoResponseParts for (T1, T2, T3, T4, T5, T6, T7)

ยง

impl<T1, T2, T3, T4, T5, T6> IntoResponseParts for (T1, T2, T3, T4, T5, T6)

ยง

impl<T1, T2, T3, T4, T5> IntoResponseParts for (T1, T2, T3, T4, T5)

ยง

impl<T1, T2, T3, T4> IntoResponseParts for (T1, T2, T3, T4)

ยง

impl<T1, T2, T3> IntoResponseParts for (T1, T2, T3)

ยง

impl<T1, T2> IntoResponseParts for (T1, T2)

ยง

impl<T1> IntoResponseParts for (T1,)

ยง

impl<T> IntoResponseParts for Option<T>

Implementorsยง

ยง

impl<I, K, V> IntoResponseParts for AppendHeaders<I>
where I: IntoIterator<Item = (K, V)>, K: TryInto<HeaderName>, <K as TryInto<HeaderName>>::Error: Display, V: TryInto<HeaderValue>, <V as TryInto<HeaderValue>>::Error: Display,

ยง

type Error = TryIntoHeaderError<<K as TryInto<HeaderName>>::Error, <V as TryInto<HeaderValue>>::Error>

ยง

impl<T> IntoResponseParts for Extension<T>
where T: Clone + Send + Sync + 'static,

ยง

type Error = !