Trait poem::web::IntoResponse

source ·
pub trait IntoResponse: Send {
    // Required method
    fn into_response(self) -> Response;

    // Provided methods
    fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
       where K: TryInto<HeaderName>,
             V: TryInto<HeaderValue>,
             Self: Sized { ... }
    fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
       where 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.

  • Xml<T>

    Sets the status to OK and the Content-Type to application/xml. Use quick-xml to serialize T into a xml 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.

§Create you own response

use poem::{
    handler, http::Uri, test::TestClient, 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)
}

let cli = TestClient::new(index);

let resp = cli.get("/").query("name", &"sunli").send().await;
resp.assert_status_is_ok();
resp.assert_text("hello sunli").await;

let resp = cli.get("/").send().await;
resp.assert_status_is_ok();
resp.assert_text("hello").await;

Required Methods§

source

fn into_response(self) -> Response

Consume itself and return Response.

Provided Methods§

source

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.

§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");
source

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.

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

let resp = "hello".with_content_type("text/abc").into_response();
assert_eq!(resp.content_type(), Some("text/abc"));
source

fn with_status(self, status: StatusCode) -> WithStatus<Self>
where Self: Sized,

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

fn with_body(self, body: impl Into<Body>) -> WithBody<Self>
where Self: Sized,

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§

source§

impl IntoResponse for &'static str

source§

impl IntoResponse for &'static [u8]

source§

impl IntoResponse for Infallible

source§

impl IntoResponse for ()

source§

impl IntoResponse for String

source§

impl IntoResponse for Vec<u8>

source§

impl IntoResponse for Bytes

source§

impl<T: IntoResponse> IntoResponse for (HeaderMap, T)

source§

impl<T: IntoResponse> IntoResponse for (StatusCode, HeaderMap, T)

source§

impl<T: IntoResponse> IntoResponse for (StatusCode, T)

Implementors§