pub struct Body { /* private fields */ }
Expand description
A type that represents an HTTP request or response body.
This type is used to represent the body of an HTTP request/response. It can be either a fixed body (e.g., a string or a byte array) or a streaming body (e.g., a large file or a database query result).
§Examples
use cot::Body;
let body = Body::fixed("Hello, world!");
let body = Body::streaming(futures::stream::once(async { Ok("Hello, world!".into()) }));
Implementations§
Source§impl Body
impl Body
Sourcepub fn fixed<T: Into<Bytes>>(data: T) -> Self
pub fn fixed<T: Into<Bytes>>(data: T) -> Self
Create a body instance with the given fixed data.
§Examples
use cot::Body;
let body = Body::fixed("Hello, world!");
Sourcepub fn streaming<T: Stream<Item = Result<Bytes>> + Send + 'static>(
stream: T,
) -> Self
pub fn streaming<T: Stream<Item = Result<Bytes>> + Send + 'static>( stream: T, ) -> Self
Create a body instance from a stream of data.
§Examples
use async_stream::stream;
use cot::Body;
let stream = stream! {
yield Ok("Hello, ".into());
yield Ok("world!".into());
};
let body = Body::streaming(stream);
Sourcepub async fn into_bytes(self) -> Result<Bytes>
pub async fn into_bytes(self) -> Result<Bytes>
Convert this Body
instance into a byte array.
This method reads the entire body into memory and returns it as a byte
array. Note that if the body is too large, this method can consume a lot
of memory. For a way to read the body while limiting the memory usage,
see Self::into_bytes_limited
.
§Errors
This method returns an error if reading the body fails.
§Examples
use cot::Body;
let body = Body::fixed("Hello, world!");
let bytes = body.into_bytes().await?;
assert_eq!(bytes, "Hello, world!".as_bytes());
Sourcepub async fn into_bytes_limited(self, limit: usize) -> Result<Bytes>
pub async fn into_bytes_limited(self, limit: usize) -> Result<Bytes>
Convert this Body
instance into a byte array.
This is a version of Self::into_bytes
that allows you to limit the
amount of memory used to read the body. If the body is larger than
the limit, an error is returned.
§Errors
This method returns an error if reading the body fails.
If the body is larger than the limit, an error is returned.
§Examples
use cot::Body;
let body = Body::fixed("Hello, world!");
let bytes = body.into_bytes_limited(32).await?;
assert_eq!(bytes, "Hello, world!".as_bytes());
Trait Implementations§
Source§impl Body for Body
impl Body for Body
Source§impl IntoResponse for Body
impl IntoResponse for Body
Source§fn into_response(self) -> Result<Response>
fn into_response(self) -> Result<Response>
cot::Result<Response>
. Read moreSource§fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
Source§fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
Content-Type
header. Read moreSource§fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
Source§fn with_body(self, body: impl Into<Body>) -> WithBody<Self>where
Self: Sized,
fn with_body(self, body: impl Into<Body>) -> WithBody<Self>where
Self: Sized,
Source§fn with_extension<T>(self, extension: T) -> WithExtension<Self, T>
fn with_extension<T>(self, extension: T) -> WithExtension<Self, T>
Auto Trait Implementations§
impl !Freeze for Body
impl !RefUnwindSafe for Body
impl Send for Body
impl Sync for Body
impl Unpin for Body
impl !UnwindSafe for Body
Blanket Implementations§
Source§impl<T> BodyExt for T
impl<T> BodyExt for T
Source§fn frame(&mut self) -> Frame<'_, Self>where
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self>where
Self: Unpin,
Frame
, if any.Source§fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
Source§fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
Source§fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
Source§fn collect(self) -> Collect<Self>where
Self: Sized,
fn collect(self) -> Collect<Self>where
Self: Sized,
Collected
body which will collect all the DATA frames
and trailers.Source§fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
Source§fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
BodyDataStream
.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db
only.