Trait axum::response::IntoResponse

source ·
pub trait IntoResponse {
    // Required method
    fn into_response(self) -> Response<UnsyncBoxBody<Bytes, Error>>;
}
Expand description

Trait for generating responses.

Types that implement IntoResponse can be returned from handlers.

§Implementing IntoResponse

You generally shouldn’t have to implement IntoResponse manually, as axum provides implementations for many common types.

However it might be necessary if you have a custom error type that you want to return from handlers:

use axum::{
    Router,
    body::{self, Bytes},
    routing::get,
    http::StatusCode,
    response::{IntoResponse, Response},
};

enum MyError {
    SomethingWentWrong,
    SomethingElseWentWrong,
}

impl IntoResponse for MyError {
    fn into_response(self) -> Response {
        let body = match self {
            MyError::SomethingWentWrong => "something went wrong",
            MyError::SomethingElseWentWrong => "something else went wrong",
        };

        // its often easiest to implement `IntoResponse` by calling other implementations
        (StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
    }
}

// `Result<impl IntoResponse, MyError>` can now be returned from handlers
let app = Router::new().route("/", get(handler));

async fn handler() -> Result<(), MyError> {
    Err(MyError::SomethingWentWrong)
}

Or if you have a custom body type you’ll also need to implement IntoResponse for it:

use axum::{
    body,
    routing::get,
    response::{IntoResponse, Response},
    Router,
};
use http_body::Body;
use http::HeaderMap;
use bytes::Bytes;
use std::{
    convert::Infallible,
    task::{Poll, Context},
    pin::Pin,
};

struct MyBody;

// First implement `Body` for `MyBody`. This could for example use
// some custom streaming protocol.
impl Body for MyBody {
    type Data = Bytes;
    type Error = Infallible;

    fn poll_data(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Option<Result<Self::Data, Self::Error>>> {
        // ...
    }

    fn poll_trailers(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>
    ) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
        // ...
    }
}

// Now we can implement `IntoResponse` directly for `MyBody`
impl IntoResponse for MyBody {
    fn into_response(self) -> Response {
        Response::new(body::boxed(self))
    }
}

// `MyBody` can now be returned from handlers.
let app = Router::new().route("/", get(|| async { MyBody }));

Required Methods§

source

fn into_response(self) -> Response<UnsyncBoxBody<Bytes, Error>>

Create a response.

Implementations on Foreign Types§

source§

impl IntoResponse for &'static str

source§

impl IntoResponse for &'static [u8]

source§

impl IntoResponse for Cow<'static, str>

source§

impl IntoResponse for Cow<'static, [u8]>

source§

impl IntoResponse for Infallible

source§

impl IntoResponse for ()

source§

impl IntoResponse for Bytes

source§

impl IntoResponse for BytesMut

source§

impl IntoResponse for String

source§

impl IntoResponse for Vec<u8>

source§

impl IntoResponse for Empty<Bytes>

source§

impl IntoResponse for Full<Bytes>

source§

impl IntoResponse for Extensions

source§

impl IntoResponse for HeaderMap

source§

impl IntoResponse for Parts

source§

impl IntoResponse for StatusCode

source§

impl<B> IntoResponse for Response<B>
where B: Body<Data = Bytes> + Send + 'static, <B as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

source§

impl<B, F> IntoResponse for MapData<B, F>
where B: Body + Send + 'static, F: FnMut(<B as Body>::Data) -> Bytes + Send + 'static, <B as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

source§

impl<B, F, E> IntoResponse for MapErr<B, F>
where B: Body<Data = Bytes> + Send + 'static, F: FnMut(<B as Body>::Error) -> E + Send + 'static, E: Into<Box<dyn Error + Sync + Send>>,

source§

impl<E> IntoResponse for BoxBody<Bytes, E>
where E: Into<Box<dyn Error + Sync + Send>> + 'static,

source§

impl<E> IntoResponse for UnsyncBoxBody<Bytes, E>
where E: Into<Box<dyn Error + Sync + Send>> + 'static,

source§

impl<K, V, const N: usize> IntoResponse for [(K, V); N]

source§

impl<R> IntoResponse for (Parts, R)
where R: IntoResponse,

source§

impl<R> IntoResponse for (Response<()>, R)
where R: IntoResponse,

source§

impl<R> IntoResponse for (StatusCode, R)
where R: IntoResponse,

source§

impl<R, T1> IntoResponse for (Parts, T1, R)

source§

impl<R, T1> IntoResponse for (Response<()>, T1, R)

source§

impl<R, T1> IntoResponse for (StatusCode, T1, R)

source§

impl<R, T1> IntoResponse for (T1, R)

source§

impl<R, T1, T2> IntoResponse for (Parts, T1, T2, R)

source§

impl<R, T1, T2> IntoResponse for (Response<()>, T1, T2, R)

source§

impl<R, T1, T2> IntoResponse for (StatusCode, T1, T2, R)

source§

impl<R, T1, T2> IntoResponse for (T1, T2, R)

source§

impl<R, T1, T2, T3> IntoResponse for (Parts, T1, T2, T3, R)

source§

impl<R, T1, T2, T3> IntoResponse for (Response<()>, T1, T2, T3, R)

source§

impl<R, T1, T2, T3> IntoResponse for (StatusCode, T1, T2, T3, R)

source§

impl<R, T1, T2, T3> IntoResponse for (T1, T2, T3, R)

source§

impl<R, T1, T2, T3, T4> IntoResponse for (Parts, T1, T2, T3, T4, R)

source§

impl<R, T1, T2, T3, T4> IntoResponse for (Response<()>, T1, T2, T3, T4, R)

source§

impl<R, T1, T2, T3, T4> IntoResponse for (StatusCode, T1, T2, T3, T4, R)

source§

impl<R, T1, T2, T3, T4> IntoResponse for (T1, T2, T3, T4, R)

source§

impl<R, T1, T2, T3, T4, T5> IntoResponse for (Parts, T1, T2, T3, T4, T5, R)

source§

impl<R, T1, T2, T3, T4, T5> IntoResponse for (Response<()>, T1, T2, T3, T4, T5, R)

source§

impl<R, T1, T2, T3, T4, T5> IntoResponse for (StatusCode, T1, T2, T3, T4, T5, R)

source§

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

source§

impl<R, T1, T2, T3, T4, T5, T6> IntoResponse for (Parts, T1, T2, T3, T4, T5, T6, R)

source§

impl<R, T1, T2, T3, T4, T5, T6> IntoResponse for (Response<()>, T1, T2, T3, T4, T5, T6, R)

source§

impl<R, T1, T2, T3, T4, T5, T6> IntoResponse for (StatusCode, T1, T2, T3, T4, T5, T6, R)

source§

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

source§

impl<R, T1, T2, T3, T4, T5, T6, T7> IntoResponse for (Parts, T1, T2, T3, T4, T5, T6, T7, R)

source§

impl<R, T1, T2, T3, T4, T5, T6, T7> IntoResponse for (Response<()>, T1, T2, T3, T4, T5, T6, T7, R)

source§

impl<R, T1, T2, T3, T4, T5, T6, T7> IntoResponse for (StatusCode, T1, T2, T3, T4, T5, T6, T7, R)

source§

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

source§

impl<R, T1, T2, T3, T4, T5, T6, T7, T8> IntoResponse for (Parts, T1, T2, T3, T4, T5, T6, T7, T8, R)

source§

impl<R, T1, T2, T3, T4, T5, T6, T7, T8> IntoResponse for (Response<()>, T1, T2, T3, T4, T5, T6, T7, T8, R)

source§

impl<R, T1, T2, T3, T4, T5, T6, T7, T8> IntoResponse for (StatusCode, T1, T2, T3, T4, T5, T6, T7, T8, R)

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl<T> IntoResponse for Result<T, ErrorResponse>
where T: IntoResponse,

source§

impl<T, E> IntoResponse for Result<T, E>

source§

impl<T, U> IntoResponse for Chain<T, U>
where T: Buf + Unpin + Send + 'static, U: Buf + Unpin + Send + 'static,

source§

impl<const N: usize> IntoResponse for &'static [u8; N]

source§

impl<const N: usize> IntoResponse for [u8; N]

Implementors§

source§

impl IntoResponse for BytesRejection

source§

impl IntoResponse for ExtensionRejection

source§

impl IntoResponse for FailedToBufferBody

source§

impl IntoResponse for FormRejection

source§

impl IntoResponse for HostRejection

source§

impl IntoResponse for PathRejection

source§

impl IntoResponse for QueryRejection

source§

impl IntoResponse for RawFormRejection

source§

impl IntoResponse for RawPathParamsRejection

source§

impl IntoResponse for StringRejection

source§

impl IntoResponse for FailedToDeserializePathParams

source§

impl IntoResponse for InvalidUtf8InPathParam

source§

impl IntoResponse for FailedToDeserializeForm

source§

impl IntoResponse for FailedToDeserializeFormBody

source§

impl IntoResponse for FailedToDeserializeQueryString

source§

impl IntoResponse for FailedToResolveHost

source§

impl IntoResponse for InvalidFormContentType

source§

impl IntoResponse for InvalidUtf8

source§

impl IntoResponse for LengthLimitError

source§

impl IntoResponse for MissingExtension

source§

impl IntoResponse for MissingPathParams

source§

impl IntoResponse for UnknownBodyError

source§

impl IntoResponse for Redirect

source§

impl<I, K, V> IntoResponse for AppendHeaders<I>

source§

impl<K, V> IntoResponse for TryIntoHeaderError<K, V>
where K: Display, V: Display,

source§

impl<S> IntoResponse for StreamBody<S>
where S: TryStream + Send + 'static, S::Ok: Into<Bytes>, S::Error: Into<BoxError>,

source§

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

source§

impl<T> IntoResponse for Html<T>
where T: Into<Full<Bytes>>,