tower_http

Module propagate_header

Source
Available on crate feature propagate-header only.
Expand description

Propagate a header from the request to the response.

§Example

use http::{Request, Response, header::HeaderName};
use std::convert::Infallible;
use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
use tower_http::propagate_header::PropagateHeaderLayer;
use bytes::Bytes;
use http_body_util::Full;

async fn handle(req: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, Infallible> {
    // ...
}

let mut svc = ServiceBuilder::new()
    // This will copy `x-request-id` headers from requests onto responses.
    .layer(PropagateHeaderLayer::new(HeaderName::from_static("x-request-id")))
    .service_fn(handle);

// Call the service.
let request = Request::builder()
    .header("x-request-id", "1337")
    .body(Full::default())?;

let response = svc.ready().await?.call(request).await?;

assert_eq!(response.headers()["x-request-id"], "1337");

Structs§