[][src]Struct http_types::Body

pub struct Body { /* fields omitted */ }

A streaming HTTP body.

Body represents the HTTP body of both Request and Response. It's completely streaming, and implements AsyncBufRead to make reading from it both convenient and performant.

Both Request and Response take Body by Into<Body>, which means that passing string literals, byte vectors, but also concrete Body instances are all valid. This makes it easy to create both quick HTTP requests, but also have fine grained control over how bodies are streamed out.

Examples

use http_types::{Body, Response, StatusCode};
use async_std::io::Cursor;

let mut req = Response::new(StatusCode::Ok);
req.set_body("Hello Chashu");

let mut req = Response::new(StatusCode::Ok);
let cursor = Cursor::new("Hello Nori");
let body = Body::from_reader(cursor, Some(10)); // set the body length
req.set_body(body);

Length

One of the details of Body to be aware of is the length parameter. The value of length is used by HTTP implementations to determine how to treat the stream. If a length is known ahead of time, it's strongly recommended to pass it.

Casting from Vec<u8>, String, or similar to Body will automatically set the value of length.

Content Encoding

By default Body will come with a fallback Mime type that is used by Request and Response if no other type has been set, and no other Mime type can be inferred.

It's strongly recommended to always set a mime type on both the Request and Response, and not rely on the fallback mechanisms. However, they're still there if you need them.

Implementations

impl Body[src]

pub fn empty() -> Self[src]

Create a new empty Body.

The body will have a length of 0, and the Mime type set to application/octet-stream if no other mime type has been set or can be sniffed.

Examples

use http_types::{Body, Response, StatusCode};

let mut req = Response::new(StatusCode::Ok);
req.set_body(Body::empty());

pub fn from_reader(
    reader: impl BufRead + Unpin + Send + Sync + 'static,
    len: Option<usize>
) -> Self
[src]

Create a Body from a reader with an optional length.

The Mime type set to application/octet-stream if no other mime type has been set or can be sniffed. If a Body has no length, HTTP implementations will often switch over to framed messages such as Chunked Encoding.

Examples

use http_types::{Body, Response, StatusCode};
use async_std::io::Cursor;

let mut req = Response::new(StatusCode::Ok);

let cursor = Cursor::new("Hello Nori");
let len = 10;
req.set_body(Body::from_reader(cursor, Some(len)));

pub fn len(&self) -> Option<usize>[src]

Get the length of the body in bytes.

Examples

use http_types::Body;
use async_std::io::Cursor;

let cursor = Cursor::new("Hello Nori");
let len = 10;
let body = Body::from_reader(cursor, Some(len));
assert_eq!(body.len(), Some(10));

pub fn is_empty(&self) -> Option<bool>[src]

Returns true if the body has a length of zero, and false otherwise.

pub fn into_reader(self) -> Box<dyn BufRead + Unpin + Send + 'static>[src]

Get the inner reader from the Body

Examples

use http_types::Body;
use async_std::io::Cursor;

let cursor = Cursor::new("Hello Nori");
let body = Body::from_reader(cursor, None);
let _ = body.into_reader();

pub async fn into_string(__arg0: Self) -> Result<String>[src]

Read the body as a string

Examples

use http_types::Body;
use async_std::io::Cursor;
  
let cursor = Cursor::new("Hello Nori");
let body = Body::from_reader(cursor, None);
assert_eq!(&body.into_string().await.unwrap(), "Hello Nori");

Trait Implementations

impl AsyncBufRead for Body[src]

impl AsyncRead for Body[src]

impl Debug for Body[src]

impl<'a> From<&'a str> for Body[src]

impl From<Request> for Body[src]

impl From<Response> for Body[src]

impl From<String> for Body[src]

impl From<Vec<u8>> for Body[src]

impl<'__pin> Unpin for Body where
    __Origin<'__pin>: Unpin
[src]

Auto Trait Implementations

impl !RefUnwindSafe for Body

impl Send for Body

impl Sync for Body

impl !UnwindSafe for Body

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> AsyncBufReadExt for R where
    R: AsyncBufRead + ?Sized
[src]

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> BufReadExt for T where
    T: AsyncBufRead + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ReadExt for T where
    T: AsyncRead + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.