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
sourceimpl Body
impl Body
sourcepub fn with_limit(&mut self, limit: usize) -> &mut Self
pub fn with_limit(&mut self, limit: usize) -> &mut Self
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)));
pub fn take(&mut self) -> Result<(Body, Option<Mime>), BodyError>
sourcepub async fn json<T: DeserializeOwned>(&mut self) -> Result<T, BodyError>
pub async fn json<T: DeserializeOwned>(&mut self) -> Result<T, BodyError>
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");
sourcepub async fn bytes(&mut self) -> Result<impl Buf, BodyError>
pub async fn bytes(&mut self) -> Result<impl Buf, BodyError>
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(&mut self) -> Result<Multipart<'_>, BodyError>
pub async fn multipart_with_constraints(
&mut self,
constraints: Constraints
) -> Result<Multipart<'_>, BodyError>
Trait Implementations
sourceimpl From<&'static [u8]> for Body
impl From<&'static [u8]> for Body
sourcefn from(data: &'static [u8]) -> Self
fn from(data: &'static [u8]) -> Self
Performs the conversion.
Auto Trait Implementations
impl !RefUnwindSafe for Body
impl Send for Body
impl Sync for Body
impl Unpin for Body
impl !UnwindSafe for Body
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more