expression

Collect HTTP trailing headers

Trailing headers follow body data and are distinct from initial headers. This synthetic body attaches one trailer and inspects it before consuming the collected data. Wire-level trailer support depends on protocol and intermediaries. In-memory fixture; no external connection. Expected stdout (not executed): complete 2 Common mistake: Expecting trailers to be present in the initial response header map.

Formal Syntax & Structure

rust syntaxrust
body.with_trailers(future).collect().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use hyper::{HeaderMap, body::Bytes};    use http_body_util::{Full, BodyExt};    let body = Full::new(Bytes::from_static(b"ok")).with_trailers(async {        let mut h = HeaderMap::new();        h.insert("x-result", "complete".parse().unwrap());        Some(Ok(h))    });    let collected = body.collect().await.unwrap();    println!("{}", collected.trailers().unwrap()["x-result"].to_str().unwrap());    println!("{}", collected.to_bytes().len());}
Rust Reference