Trait poem::web::IntoResponse[][src]

pub trait IntoResponse: Send {
    fn into_response(self) -> Response;

    fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
    where
        K: TryInto<HeaderName>,
        V: TryInto<HeaderValue>,
        Self: Sized
, { ... }
fn with_status(self, status: StatusCode) -> WithStatus<Self>
    where
        Self: Sized
, { ... }
fn with_body(self, body: impl Into<Body>) -> WithBody<Self>
    where
        Self: Sized
, { ... } }
Expand description

Represents a type that can convert into response.

Provided Implementations

  • ()

    Sets the status to OK with an empty body.

  • &’static str

    Sets the status to OK and the Content-Type to text/plain. The string is used as the body of the response.

  • String

    Sets the status to OK and the Content-Type to text/plain. The string is used as the body of the response.

  • &’static u8

    Sets the status to OK and the Content-Type to application/octet-stream. The slice is used as the body of the response.

  • Html<T>

    Sets the status to OK and the Content-Type to text/html. T is used as the body of the response.

  • Json<T>

    Sets the status to OK and the Content-Type to application/json. Use serde_json to serialize T into a json string.

  • Bytes

    Sets the status to OK and the Content-Type to application/octet-stream. The bytes is used as the body of the response.

  • Vec<u8>

    Sets the status to OK and the Content-Type to application/octet-stream. The vector’s data is used as the body of the response.

  • Body

    Sets the status to OK and use the specified body.

  • StatusCode

    Sets the status to the specified status code StatusCode with an empty body.

  • (StatusCode, T)

    Convert T to response and set the specified status code StatusCode.

  • (StatusCode, HeaderMap, T)

    Convert T to response and set the specified status code StatusCode, and then merge the specified HeaderMap.

  • Response

    The implementation for Response always returns itself.

  • Compress<T>

    Call T::into_response to get the response, then compress the response body with the specified algorithm, and set the correct Content-Encoding header.

  • SSE

    Sets the status to OK and the Content-Type to text/event-stream with an event stream body. Use the SSE::new function to create it.

Custom response

use poem::{handler, http::Uri, web::Query, Endpoint, IntoResponse, Request, Response};
use serde::Deserialize;

struct Hello(Option<String>);

impl IntoResponse for Hello {
    fn into_response(self) -> Response {
        let msg = match self.0 {
            Some(name) => format!("hello {}", name),
            None => format!("hello"),
        };
        msg.into_response()
    }
}

#[derive(Deserialize)]
struct Params {
    name: Option<String>,
}

#[handler]
async fn index(params: Query<Params>) -> impl IntoResponse {
    Hello(params.0.name)
}

assert_eq!(
    index
        .call(
            Request::builder()
                .uri(Uri::from_static("/?name=sunli"))
                .finish()
        )
        .await
        .take_body()
        .into_string()
        .await
        .unwrap(),
    "hello sunli"
);

assert_eq!(
    index
        .call(Request::builder().uri(Uri::from_static("/")).finish())
        .await
        .take_body()
        .into_string()
        .await
        .unwrap(),
    "hello"
);

Required methods

Consume itself and return Response.

Provided methods

Wrap an impl IntoResponse to add a header.

Example
use poem::{http::HeaderValue, IntoResponse};

let resp = "hello".with_header("foo", "bar").into_response();
assert_eq!(
    resp.headers().get("foo"),
    Some(&HeaderValue::from_static("bar"))
);
assert_eq!(resp.into_body().into_string().await.unwrap(), "hello");

Wrap an impl IntoResponse to set a status code.

Example
use poem::{http::StatusCode, IntoResponse};

let resp = "hello".with_status(StatusCode::CONFLICT).into_response();
assert_eq!(resp.status(), StatusCode::CONFLICT);
assert_eq!(resp.into_body().into_string().await.unwrap(), "hello");

Wrap an impl IntoResponse to set a body.

Example
use poem::{http::StatusCode, IntoResponse};

let resp = StatusCode::CONFLICT.with_body("hello").into_response();
assert_eq!(resp.status(), StatusCode::CONFLICT);
assert_eq!(resp.into_body().into_string().await.unwrap(), "hello");

Implementations on Foreign Types

Implementors