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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Create a response.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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