Module response
Types and traits for generating responses.
Building responses
Anything that implements IntoResponse can be returned from a handler. axum
provides implementations for common types:
use ;
// `()` gives an empty response
async
// String will get a `text/plain; charset=utf-8` content-type
async
// Bytes will get a `application/octet-stream` content-type
async
// `Json` will get a `application/json` content-type and work with anything that
// implements `serde::Serialize`
async
// `Html` will get a `text/html` content-type
async
// `StatusCode` gives an empty response with that status code
async
// `HeaderMap` gives an empty response with some headers
async
// An array of tuples also gives headers
async
// Use `impl IntoResponse` to avoid writing the whole type
async
Additionally you can return tuples to build more complex responses from individual parts.
use ;
// `(StatusCode, impl IntoResponse)` will override the status code of the response
async
// Use `impl IntoResponse` to avoid having to type the whole type
async
// `(HeaderMap, impl IntoResponse)` to add additional headers
async
// Or an array of tuples to more easily build the headers
async
// Use string keys for custom headers
async
// `(StatusCode, headers, impl IntoResponse)` to set status and add headers
// `headers` can be either a `HeaderMap` or an array of tuples
async
// `(Extension<_>, impl IntoResponse)` to set response extensions
async
;
// Or mix and match all the things
async
In general you can return tuples like:
(StatusCode, impl IntoResponse)(Parts, impl IntoResponse)(Response<()>, impl IntoResponse)(T1, .., Tn, impl IntoResponse)whereT1toTnall implementIntoResponseParts.(StatusCode, T1, .., Tn, impl IntoResponse)whereT1toTnall implementIntoResponseParts.(Parts, T1, .., Tn, impl IntoResponse)whereT1toTnall implementIntoResponseParts.(Response<()>, T1, .., Tn, impl IntoResponse)whereT1toTnall implementIntoResponseParts.
This means you cannot accidentally override the status or body as IntoResponseParts only allows
setting headers and extensions.
Use Response for more low level control:
use ;
async
Returning different response types
If you need to return multiple response types, and Result<T, E> isn't appropriate, you can call
.into_response() to turn things into axum::response::Response:
use ;
async
Regarding impl IntoResponse
You can use impl IntoResponse as the return type from handlers to avoid
typing large types. For example
use StatusCode;
async
Becomes easier using impl IntoResponse:
use ;
async
However impl IntoResponse has a few limitations. Firstly it can only be used
to return a single type:
use ;
async
This function returns either a StatusCode or a &'static str which impl Trait doesn't allow.
Secondly impl IntoResponse can lead to type inference issues when used with
Result and ?:
use ;
async
This is because ? supports using the From trait to convert to a different
error type but it doesn't know which type to convert to, because we only
specified impl IntoResponse as the return type.
Result<impl IntoResponse, impl IntoResponse> doesn't always work either:
use ;
async
The solution is to use a concrete error type, such as Result<impl IntoResponse, StatusCode>:
use ;
async
Because of this it is generally not recommended to use impl IntoResponse
unless you're familiar with the details of how impl Trait works.
Modules
- sse Server-Sent Events (SSE) responses.