rama::http::layer

Module propagate_headers

Expand description

Propagate a header from the request to the response.

§Example

use std::convert::Infallible;
use rama_core::error::BoxError;
use rama_core::service::service_fn;
use rama_core::{Context, Service, Layer};
use rama_http::{Body, Request, Response, header::HeaderName};
use rama_http::layer::propagate_headers::PropagateHeaderLayer;

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

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

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

let response = svc.serve(Context::default(), request).await?;

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

Structs§