Struct poem::Body

source · []
pub struct Body(_);
Expand description

A body object for requests and responses.

Implementations

Create a body object from Bytes.

Create a body object from String.

Create a body object from Vec<u8>.

Create a body object from reader.

Create a body object from bytes stream.

Create a body object from JSON.

Create an empty body.

Returns true if this body is empty.

Consumes this body object to return a Bytes that contains all data.

Consumes this body object to return a Vec<u8> that contains all data.

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
);

Consumes this body object to return a String that contains all data.

Consumes this body object and parse it as T.

Errors

Consumes this body object and parse it as T.

Errors

Consumes this body object to return a reader.

Consumes this body object to return a bytes stream.

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Extract from request head and body.
Extract from request head. Read more
Consume itself and return Response.
Wrap an impl IntoResponse to add a header. Read more
Wrap an impl IntoResponse to with a new content type. Read more
Wrap an impl IntoResponse to set a status code. Read more
Wrap an impl IntoResponse to set a body. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Consumes this value returns a poem::Result<T>.
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more