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, Headers, 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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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.