pub struct Body(/* private fields */);Expand description
A HTTP Body.
Construct this HTTP body using:
Body::emptyfor the empty body, orimpl From<()> for BodyFrom<&[u8]>(which will make a clone) orFrom<Vec<u8>>orFrom<Bytes>for aBodyfrom bytes.From<&str>(which will make a clone) orFrom<String>for aBodyfrom strings.Body::from_jsonfor aBodyfrom aSerialize(requires featurejson)From<AsyncInputStream>for aBodywith contents given by the contents of a WASI input-stream.Body::from_streamorBody::from_try_streamfor aBodyfrom aStreamofInto<Bytes>
Consume this HTTP body using:
Body::into_boxed_bodyconverts it to anUnsyncBoxBody<Bytes, Error>. This is a boxed representation ofhttp_body::Bodythat isSendbut notSync. The Unsync variant is required for compatibility with theaxumcrate.async fn Body::contents(&mut self) -> Result<&[u8], Error>is ready when all contents of the body have been collected, and gives them as a byte slice.async fn Body::bytes_contents(&mut self) -> Result<Bytes, Error>is the same asBody::contents, except returns theBytestype instead of a byte slice.async fn Body::str_contents(&mut self) -> Result<&str, Error>is ready when all contents of the body have been collected, and gives them as a str slice.async fn Body::json(&mut self) -> Result<T, Error>gathers body contents and then usesT: serde::Deserializeto deserialize to json (requires featurejson).
Implementations§
Source§impl Body
impl Body
Sourcepub fn into_boxed_body(self) -> UnsyncBoxBody<Bytes, Error>
pub fn into_boxed_body(self) -> UnsyncBoxBody<Bytes, Error>
Convert this Body into an UnsyncBoxBody<Bytes, Error>, which
exists to implement the http_body::Body trait. Consume the contents
using http_body_utils::BodyExt, or anywhere else an impl of
http_body::Body is accepted.
Sourcepub async fn contents(&mut self) -> Result<&[u8], Error>
pub async fn contents(&mut self) -> Result<&[u8], Error>
Collect the entire contents of this Body, and expose them as a
byte slice. This async fn will be pending until the entire Body is
copied into memory, or an error occurs.
Sourcepub async fn bytes_contents(&mut self) -> Result<Bytes, Error>
pub async fn bytes_contents(&mut self) -> Result<Bytes, Error>
Collect the entire contents of this Body, and expose it as Bytes.
This async fn will be pending until the entire Body is
copied into memory, or an error occurs.
Sourcepub fn content_length(&self) -> Option<u64>
pub fn content_length(&self) -> Option<u64>
Get a value for the length of this Body’s content, in bytes, if
known. This value can come from either the Content-Length header
received in the incoming request or response assocated with the body,
or be provided by an exact http_body::Body::size_hint if the Body
is constructed from an http_body::Body impl.
Sourcepub async fn str_contents(&mut self) -> Result<&str, Error>
pub async fn str_contents(&mut self) -> Result<&str, Error>
Collect the entire contents of this Body, and expose them as a
string slice. This async fn will be pending until the entire Body is
copied into memory, or an error occurs. Additonally errors if the
contents of the Body were not a utf-8 encoded string.
Sourcepub fn from_json<T: Serialize>(data: &T) -> Result<Self, Error>
pub fn from_json<T: Serialize>(data: &T) -> Result<Self, Error>
Construct a Body by serializing a type to json. Can fail with a
serde_json::Error if serilization fails.
Sourcepub async fn json<T: for<'a> Deserialize<'a>>(&mut self) -> Result<T, Error>
pub async fn json<T: for<'a> Deserialize<'a>>(&mut self) -> Result<T, Error>
Collect the entire contents of this Body, and deserialize them from
json. Can fail if the body contents are not utf-8 encoded, are not
valid json, or the json is not accepted by the serde::Deserialize impl.
Sourcepub fn from_stream<S>(stream: S) -> Self
pub fn from_stream<S>(stream: S) -> Self
Construct a Body backed by a futures_lite::Stream impl. The stream
will be polled as the body is sent.
Sourcepub fn from_try_stream<S, D, E>(stream: S) -> Self
pub fn from_try_stream<S, D, E>(stream: S) -> Self
Construct a Body backed by a futures_lite::Stream impl. The stream
will be polled as the body is sent. If the stream gives an error, the
body will canceled, which closes the underlying connection.
Sourcepub fn from_http_body<B>(http_body: B) -> Self
pub fn from_http_body<B>(http_body: B) -> Self
Construct a Body backed by a http_body::Body. The http_body will
be polled as the body is sent. If the http_body poll gives an error,
the body will be canceled, which closes the underlying connection.
Note, this is the only constructor which permits adding trailers to
the Body.