pub struct Body(_);
Expand description
A body object for requests and responses.
Implementations
sourceimpl 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 where
S: Stream<Item = Result<O, E>> + Send + 'static,
O: Into<Bytes> + 'static,
E: Error + Send + Sync + 'static,
pub fn from_bytes_stream<S, O, E>(stream: S) -> Self where
S: Stream<Item = Result<O, E>> + Send + 'static,
O: Into<Bytes> + 'static,
E: Error + Send + Sync + 'static,
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 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
sourceimpl From<&'static [u8]> for Body
impl From<&'static [u8]> for Body
sourcefn from(data: &'static [u8]) -> Self
fn from(data: &'static [u8]) -> Self
Performs the conversion.
sourceimpl<'a> FromRequest<'a> for Body
impl<'a> FromRequest<'a> for Body
sourcefn from_request<'life0, 'async_trait>(
_req: &'a Request,
body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>> where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
_req: &'a Request,
body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>> where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Extract from request head and body.
sourceimpl IntoResponse for Body
impl IntoResponse for Body
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Consume itself and return Response
.
sourcefn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self> where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Self: Sized,
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self> where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Self: Sized,
Wrap an impl IntoResponse
to add a header. Read more
sourcefn with_content_type<V>(self, content_type: V) -> WithContentType<Self> where
V: TryInto<HeaderValue>,
Self: Sized,
fn with_content_type<V>(self, content_type: V) -> WithContentType<Self> where
V: TryInto<HeaderValue>,
Self: Sized,
Wrap an impl IntoResponse
to with a new content type. Read more
sourcefn 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 more
Auto Trait Implementations
impl !RefUnwindSafe for Body
impl Send for Body
impl Sync for Body
impl Unpin for Body
impl !UnwindSafe for Body
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoResult<T> for T where
T: IntoResponse,
impl<T> IntoResult<T> for T where
T: IntoResponse,
sourcefn into_result(self) -> Result<T, Error>
fn into_result(self) -> Result<T, Error>
Consumes this value returns a poem::Result<T>
.
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more