Struct solarsail::body::Body [−][src]
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
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"[..]);
pub async fn multipart_with_constraints(
&mut self,
constraints: Constraints
) -> Result<Multipart<'_>, BodyError>
Trait Implementations
Performs the conversion.