[−][src]Struct tide::Body
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() -> Body
[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 Send + Sync + Unpin + AsyncBufRead + 'static,
len: Option<usize>
) -> Body
[src]
reader: impl Send + Sync + Unpin + AsyncBufRead + 'static,
len: Option<usize>
) -> Body
Create a Body
from a reader with an optional length.
The Mime type is 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 into_reader(
self
) -> Box<dyn AsyncBufRead + 'static + Unpin + Sync + Send>
[src]
self
) -> Box<dyn AsyncBufRead + 'static + Unpin + Sync + Send>
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 fn from_bytes(bytes: Vec<u8>) -> Body
[src]
Create a Body
from a Vec of bytes.
The Mime type is 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 input = vec![1, 2, 3]; req.set_body(Body::from_bytes(input));
pub async fn into_bytes(__arg0: Self) -> Result<Vec<u8>, Error>
[src]
Parse the body into a Vec<u8>
.
Examples
use http_types::Body; let bytes = vec![1, 2, 3]; let body = Body::from_bytes(bytes); let bytes: Vec<u8> = body.into_bytes().await?; assert_eq!(bytes, vec![1, 2, 3]);
pub fn from_string(s: String) -> Body
[src]
Create a Body
from a String
The Mime type is set to text/plain
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 input = String::from("hello Nori!"); req.set_body(Body::from_string(input));
pub async fn into_string(__arg0: Self) -> Result<String, Error>
[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");
pub fn from_json(json: &impl Serialize) -> Result<Body, Error>
[src]
Creates a Body
from a type, serializing it as JSON.
Mime
The encoding is set to application/json
.
Examples
use http_types::{Body, convert::json}; let body = Body::from_json(&json!({ "name": "Chashu" }));
pub async fn into_json<T>(__arg0: Self) -> Result<T, Error> where
T: DeserializeOwned,
[src]
T: DeserializeOwned,
Parse the body as JSON, serializing it to a struct.
Examples
use http_types::Body; use http_types::convert::{Serialize, Deserialize}; #[derive(Debug, Serialize, Deserialize)] struct Cat { name: String } let cat = Cat { name: String::from("chashu") }; let body = Body::from_json(&cat)?; let cat: Cat = body.into_json().await?; assert_eq!(&cat.name, "chashu");
pub fn from_form(form: &impl Serialize) -> Result<Body, Error>
[src]
Creates a Body
from a type, serializing it using form encoding.
Mime
The encoding is set to application/x-www-form-urlencoded
.
Errors
An error will be returned if the encoding failed.
Examples
use http_types::Body; use http_types::convert::{Serialize, Deserialize}; #[derive(Debug, Serialize, Deserialize)] struct Cat { name: String } let cat = Cat { name: String::from("chashu") }; let body = Body::from_form(&cat)?; let cat: Cat = body.into_form().await?; assert_eq!(&cat.name, "chashu");
pub async fn into_form<T>(self) -> Result<T, Error> where
T: DeserializeOwned,
[src]
T: DeserializeOwned,
Parse the body from form encoding into a type.
Errors
An error is returned if the underlying IO stream errors, or if the body could not be deserialized into the type.
Examples
use http_types::Body; use http_types::convert::{Serialize, Deserialize}; #[derive(Debug, Serialize, Deserialize)] struct Cat { name: String } let cat = Cat { name: String::from("chashu") }; let body = Body::from_form(&cat)?; let cat: Cat = body.into_form().await?; assert_eq!(&cat.name, "chashu");
pub async fn from_file<P>(path: P) -> Result<Body, Error> where
P: AsRef<Path>,
[src]
P: AsRef<Path>,
Create a Body
from a file.
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 res = Response::new(StatusCode::Ok); res.set_body(Body::from_file("/path/to/file").await?);
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 mime(&self) -> &Mime
[src]
Returns the mime type of this Body.
pub fn set_mime(&mut self, mime: impl Into<Mime>)
[src]
Sets the mime type of this Body.
Trait Implementations
impl AsyncBufRead for Body
[src]
fn poll_fill_buf(
self: Pin<&mut Body>,
cx: &mut Context
) -> Poll<Result<&[u8], Error>>
[src]
self: Pin<&mut Body>,
cx: &mut Context
) -> Poll<Result<&[u8], Error>>
fn consume(self: Pin<&mut Body>, amt: usize)
[src]
impl AsyncRead for Body
[src]
fn poll_read(
self: Pin<&mut Body>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Body>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
impl Debug for Body
[src]
impl<'a> From<&'a [u8]> for Body
[src]
impl<'a> From<&'a str> for Body
[src]
impl From<Body> for Response
[src]
impl From<Request> for Body
[src]
impl From<String> for Body
[src]
impl From<Value> for Body
[src]
impl From<Vec<u8>> for Body
[src]
impl<'__pin> Unpin for Body where
__Origin<'__pin>: Unpin,
[src]
__Origin<'__pin>: Unpin,
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
[src]
R: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn lines(self) -> Lines<Self>
[src]
impl<R> AsyncBufReadExt for R where
R: AsyncBufRead + ?Sized,
R: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn lines(self) -> Lines<Self> where
Self: Unpin,
Self: Unpin,
fn split(self, byte: u8) -> Split<Self>
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
Self: AsyncWrite,
[src]
Self: AsyncWrite,
fn take(self, limit: u64) -> Take<Self>
[src]
fn compat(self) -> Compat<Self> where
Self: Unpin,
[src]
Self: Unpin,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
R: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
fn take(self, limit: u64) -> Take<Self>
fn bytes(self) -> Bytes<Self>
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
R: AsyncRead,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> BufReadExt for T where
T: AsyncBufRead + ?Sized,
[src]
T: AsyncBufRead + ?Sized,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'a, Self> where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn lines(self) -> Lines<Self> where
Self: Unpin,
[src]
Self: Unpin,
fn split(self, byte: u8) -> Split<Self>
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ReadExt for T where
T: AsyncRead + ?Sized,
[src]
T: AsyncRead + ?Sized,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn take(self, limit: u64) -> Take<Self>
[src]
fn by_ref(&mut self) -> &mut Self
[src]
fn bytes(self) -> Bytes<Self>
[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> Sealed<T> for T where
T: ?Sized,
T: ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,