pub struct Headers<H>(pub H);
Expand description
A response with headers.
Example
use axum::{
Router,
response::{IntoResponse, Headers},
routing::get,
};
use http::header::{HeaderName, HeaderValue};
// It works with any `IntoIterator<Item = (Key, Value)>` where `Key` can be
// turned into a `HeaderName` and `Value` can be turned into a `HeaderValue`
//
// Such as `Vec<(HeaderName, HeaderValue)>`
async fn just_headers() -> impl IntoResponse {
Headers(vec![
(HeaderName::from_static("X-Foo"), HeaderValue::from_static("foo")),
])
}
// Or `Vec<(&str, &str)>`
async fn from_strings() -> impl IntoResponse {
Headers(vec![("X-Foo", "foo")])
}
// Or `[(&str, &str)]` if you're on Rust 1.53+
let app = Router::new()
.route("/just-headers", get(just_headers))
.route("/from-strings", get(from_strings));
If a conversion to HeaderName
or HeaderValue
fails a 500 Internal Server Error
response will be returned.
You can also return (Headers, impl IntoResponse)
to customize the headers
of a response, or (StatusCode, Headeres, impl IntoResponse)
to customize
the status code and headers.
Tuple Fields
0: H
Trait Implementations
impl<H, K, V> IntoResponse for Headers<H> where
H: IntoIterator<Item = (K, V)>,
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
<K as TryInto<HeaderName>>::Error: Display,
<V as TryInto<HeaderValue>>::Error: Display,
impl<H, K, V> IntoResponse for Headers<H> where
H: IntoIterator<Item = (K, V)>,
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
<K as TryInto<HeaderName>>::Error: Display,
<V as TryInto<HeaderValue>>::Error: Display,
Create a response.
Auto Trait Implementations
impl<H> RefUnwindSafe for Headers<H> where
H: RefUnwindSafe,
impl<H> UnwindSafe for Headers<H> where
H: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Create a new Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more