Add Headers response (#193)

* Add `Headers`

Example usage:

```rust
use axum::{
    route,
    routing::RoutingDsl,
    response::{IntoResponse, Headers},
    handler::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 `[(&str, &str)]`
async fn from_strings() -> impl IntoResponse {
    Headers([("X-Foo", "foo")])
}
```

Fixes https://github.com/tokio-rs/axum/issues/187

* Make work on Rust versions without `IntoIterator` for arrays

* format

* changelog
This commit is contained in:
David Pedersen
2021-08-17 17:28:02 +02:00
committed by GitHub
parent baa99e5084
commit 97c140cdf7
4 changed files with 243 additions and 8 deletions
+11 -6
View File
@@ -13,15 +13,20 @@ use http_body::{
use std::{borrow::Cow, convert::Infallible};
use tower::{util::Either, BoxError};
mod headers;
mod redirect;
pub mod sse;
#[doc(no_inline)]
pub use crate::Json;
mod redirect;
pub use self::redirect::Redirect;
pub mod sse;
pub use sse::{sse, Sse};
#[doc(inline)]
pub use self::{
headers::Headers,
redirect::Redirect,
sse::{sse, Sse},
};
/// Trait for generating responses.
///