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 theContent-Type
totext/plain
. The string is used as the body of the response. -
String
Sets the status to
OK
and theContent-Type
totext/plain
. The string is used as the body of the response. -
&’static u8
Sets the status to
OK
and theContent-Type
toapplication/octet-stream
. The slice is used as the body of the response. -
Html<T>
Sets the status to
OK
and theContent-Type
totext/html
.T
is used as the body of the response. -
Json<T>
Sets the status to
OK
and theContent-Type
toapplication/json
. Useserde_json
to serializeT
into a json string. -
Xml<T>
Sets the status to
OK
and theContent-Type
toapplication/xml
. Usequick-xml
to serializeT
into a xml string. -
Bytes
Sets the status to
OK
and theContent-Type
toapplication/octet-stream
. The bytes is used as the body of the response. -
Vec<u8>
Sets the status to
OK
and theContent-Type
toapplication/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 codeStatusCode
. -
(StatusCode, HeaderMap, T)
Convert
T
to response and set the specified status codeStatusCode
, and then merge the specifiedHeaderMap
. -
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 correctContent-Encoding
header. -
SSE
Sets the status to
OK
and theContent-Type
totext/event-stream
with an event stream body. Use theSSE::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§
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Consume itself and return Response
.
Provided Methods§
sourcefn 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.
§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");
sourcefn 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.
§Example
use poem::{http::HeaderValue, IntoResponse};
let resp = "hello".with_content_type("text/abc").into_response();
assert_eq!(resp.content_type(), Some("text/abc"));
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.
§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");
sourcefn with_body(self, body: impl Into<Body>) -> WithBody<Self>where
Self: Sized,
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
impl IntoResponse for &'static str
fn into_response(self) -> Response
source§impl IntoResponse for &'static [u8]
impl IntoResponse for &'static [u8]
fn into_response(self) -> Response
source§impl IntoResponse for Infallible
impl IntoResponse for Infallible
fn into_response(self) -> Response
source§impl IntoResponse for ()
impl IntoResponse for ()
fn into_response(self) -> Response
source§impl IntoResponse for String
impl IntoResponse for String
fn into_response(self) -> Response
source§impl IntoResponse for Vec<u8>
impl IntoResponse for Vec<u8>
fn into_response(self) -> Response
source§impl IntoResponse for Bytes
impl IntoResponse for Bytes
fn into_response(self) -> Response
source§impl<T: IntoResponse> IntoResponse for (HeaderMap, T)
impl<T: IntoResponse> IntoResponse for (HeaderMap, T)
fn into_response(self) -> Response
source§impl<T: IntoResponse> IntoResponse for (StatusCode, HeaderMap, T)
impl<T: IntoResponse> IntoResponse for (StatusCode, HeaderMap, T)
fn into_response(self) -> Response
source§impl<T: IntoResponse> IntoResponse for (StatusCode, T)
impl<T: IntoResponse> IntoResponse for (StatusCode, T)
fn into_response(self) -> Response
Implementors§
impl IntoResponse for StaticFileResponse
impl IntoResponse for StatusCode
impl IntoResponse for Body
impl IntoResponse for Response
impl IntoResponse for SSE
sse
only.impl IntoResponse for Redirect
impl<F, Fut> IntoResponse for WebSocketUpgraded<F>
websocket
only.