pub struct Body(_);
Expand description

The body of a HTTP request and response. It is internally based on hyper::Body, but adds convenient methods around reading the body into certain formats. A body can only be read once. Once read, each subsequent attempt to read the body will return a BodyError::AlreadyRead.

Implementations

Create an empty Body.

Creates a new Body with the given limit set. The limit restricts the reading of a request body with a content-length (header) greater the limit. The default limit is set to one 1MiB.

Example
let mut req = http::Request::builder().body(Body::from(vec![0; 16]))?;
assert!(matches!(req.body_mut().with_limit(8).bytes().await, Err(BodyError::MaxSize)));

Reads and deserializes the whole body as JSON.

Example
let mut req = http::Request::builder().body(Body::from(r#"{"name":"SolarSail"}"#))?;
#[derive(Deserialize)]
struct Info {
  name: String,
}
let info: Info = req.body_mut().json().await?;
assert_eq!(&info.name, "SolarSail");

Reads the whole body as raw bytes.

Example
let mut req = http::Request::builder().body(Body::from(&b"SolarSail"[..]))?;
let mut buf = req.body_mut().bytes().await?;
let bytes = buf.copy_to_bytes(9);
assert_eq!(&bytes[..], &b"SolarSail"[..]);

Trait Implementations

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more