Skip to main content

Body

Struct Body 

Source
pub struct Body(/* private fields */);
Expand description

A HTTP Body.

Construct this HTTP body using:

  • Body::empty for the empty body, or impl From<()> for Body
  • From<&[u8]> (which will make a clone) or From<Vec<u8>> or From<Bytes> for a Body from bytes.
  • From<&str> (which will make a clone) or From<String> for a Body from strings.
  • Body::from_json for a Body from a Serialize (requires feature json)
  • From<AsyncInputStream> for a Body with contents given by the contents of a WASI input-stream.
  • Body::from_stream or Body::from_try_stream for a Body from a Stream of Into<Bytes>

Consume this HTTP body using:

  • Body::into_boxed_body converts it to an UnsyncBoxBody<Bytes, Error>. This is a boxed representation of http_body::Body that is Send but not Sync. The Unsync variant is required for compatibility with the axum crate.
  • 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 as Body::contents, except returns the Bytes type 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 uses T: serde::Deserialize to deserialize to json (requires feature json).

Implementations§

Source§

impl Body

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn empty() -> Self

Construct an empty Body

Source

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.

Source

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.

Source

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.

Source

pub fn from_stream<S>(stream: S) -> Self
where S: Stream + Send + 'static, <S as Stream>::Item: Into<Bytes>,

Construct a Body backed by a futures_lite::Stream impl. The stream will be polled as the body is sent.

Source

pub fn from_try_stream<S, D, E>(stream: S) -> Self
where S: Stream<Item = Result<D, E>> + Send + 'static, D: Into<Bytes>, E: Error + Send + Sync + 'static,

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.

Source

pub fn from_http_body<B>(http_body: B) -> Self
where B: HttpBody + Send + 'static, <B as HttpBody>::Data: Into<Bytes>, <B as HttpBody>::Error: Into<Error>,

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.

Trait Implementations§

Source§

impl Debug for Body

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&[u8]> for Body

Source§

fn from(bytes: &[u8]) -> Body

Converts to this type from the input type.
Source§

impl From<&str> for Body

Source§

fn from(data: &str) -> Body

Converts to this type from the input type.
Source§

impl From<()> for Body

Source§

fn from(_: ()) -> Body

Converts to this type from the input type.
Source§

impl From<AsyncInputStream> for Body

Source§

fn from(r: AsyncInputStream) -> Body

Converts to this type from the input type.
Source§

impl From<Bytes> for Body

Source§

fn from(data: Bytes) -> Body

Converts to this type from the input type.
Source§

impl From<String> for Body

Source§

fn from(data: String) -> Body

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Body

Source§

fn from(bytes: Vec<u8>) -> Body

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !Freeze for Body

§

impl !RefUnwindSafe for Body

§

impl !Sync for Body

§

impl !UnwindSafe for Body

§

impl Send for Body

§

impl Unpin for Body

§

impl UnsafeUnpin for Body

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.