Module http_body_util

Body types and combinators for hyper and the http-body ecosystem.


http-body-util provides the concrete request and response body types that hyper 1.x leaves out. hyper defines only the Body trait and Incoming, the body type of received messages. Anything that sends or reads body bytes needs this crate.

The types used most:

Examples

Creating bodies and reading them back with collect:

use bytes::Bytes;
use futures::executor::block_on;
use http_body_util::{BodyExt, Empty, Full};

let body = Full::new(Bytes::from("hello"));
let bytes = block_on(body.collect()).unwrap().to_bytes();
assert_eq!(bytes, "hello");

let empty = Empty::<Bytes>::new();
let bytes = block_on(empty.collect()).unwrap().to_bytes();
assert!(bytes.is_empty());

Returning different body types as one BoxBody:

use bytes::Bytes;
use futures::executor::block_on;
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
use std::convert::Infallible;

fn body_for(found: bool) -> BoxBody<Bytes, Infallible> {
    if found {
        Full::new(Bytes::from("found")).boxed()
    } else {
        Empty::new().boxed()
    }
}

let bytes = block_on(body_for(true).collect()).unwrap().to_bytes();
assert_eq!(bytes, "found");

Modules

Structs

Enums

Traits