pub struct Body(/* private fields */);
Expand description
A body object for requests and responses.
Implementations§
source§impl Body
impl Body
sourcepub fn from_bytes(data: Bytes) -> Self
pub fn from_bytes(data: Bytes) -> Self
Create a body object from Bytes
.
sourcepub fn from_string(data: String) -> Self
pub fn from_string(data: String) -> Self
Create a body object from String
.
sourcepub fn from_async_read(reader: impl AsyncRead + Send + 'static) -> Self
pub fn from_async_read(reader: impl AsyncRead + Send + 'static) -> Self
Create a body object from reader.
sourcepub fn from_bytes_stream<S, O, E>(stream: S) -> Self
pub fn from_bytes_stream<S, O, E>(stream: S) -> Self
Create a body object from bytes stream.
sourcepub async fn into_bytes(self) -> Result<Bytes, ReadBodyError>
pub async fn into_bytes(self) -> Result<Bytes, ReadBodyError>
Consumes this body object to return a Bytes
that contains all data.
sourcepub async fn into_vec(self) -> Result<Vec<u8>, ReadBodyError>
pub async fn into_vec(self) -> Result<Vec<u8>, ReadBodyError>
Consumes this body object to return a Vec<u8>
that contains all
data.
sourcepub async fn into_bytes_limit(
self,
limit: usize
) -> Result<Bytes, ReadBodyError>
pub async fn into_bytes_limit( self, limit: usize ) -> Result<Bytes, ReadBodyError>
Consumes this body object to return a Bytes
that contains all
data, returns Err(ReadBodyError::PayloadTooLarge)
if the length of the
payload exceeds limit
.
§Example
use poem::{error::ReadBodyError, handler, http::StatusCode, Body, Endpoint, Request, Result};
#[handler]
async fn index(data: Body) -> Result<()> {
Ok(data.into_bytes_limit(5).await.map(|_| ())?)
}
let req = Request::builder().body("12345");
assert_eq!(index.get_response(req).await.status(), StatusCode::OK);
let req = Request::builder().body("123456");
assert_eq!(
index.get_response(req).await.status(),
StatusCode::PAYLOAD_TOO_LARGE
);
sourcepub async fn into_string(self) -> Result<String, ReadBodyError>
pub async fn into_string(self) -> Result<String, ReadBodyError>
Consumes this body object to return a String
that contains all data.
sourcepub async fn into_json<T: DeserializeOwned>(self) -> Result<T>
pub async fn into_json<T: DeserializeOwned>(self) -> Result<T>
sourcepub async fn into_xml<T: DeserializeOwned>(self) -> Result<T>
pub async fn into_xml<T: DeserializeOwned>(self) -> Result<T>
sourcepub fn into_async_read(self) -> impl AsyncRead + Unpin + Send + 'static
pub fn into_async_read(self) -> impl AsyncRead + Unpin + Send + 'static
Consumes this body object to return a reader.
Trait Implementations§
source§impl<'a> FromRequest<'a> for Body
impl<'a> FromRequest<'a> for Body
source§async fn from_request(_req: &'a Request, body: &mut RequestBody) -> Result<Self>
async fn from_request(_req: &'a Request, body: &mut RequestBody) -> Result<Self>
Extract from request head and body.
source§impl IntoResponse for Body
impl IntoResponse for Body
source§fn into_response(self) -> Response
fn into_response(self) -> Response
Consume itself and return
Response
.source§fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
Wrap an
impl IntoResponse
to add a header. Read moresource§fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
Wrap an
impl IntoResponse
to with a new content type. Read moresource§fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
Wrap an
impl IntoResponse
to set a status code. Read moreAuto 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<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> IntoResult<T> for Twhere
T: IntoResponse,
impl<T> IntoResult<T> for Twhere
T: IntoResponse,
source§fn into_result(self) -> Result<T, Error>
fn into_result(self) -> Result<T, Error>
Consumes this value returns a
poem::Result<T>
.source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
Available on crate feature
tower-compat
only.Converts a tower service to a poem endpoint.