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 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. -
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, 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
.unwrap()
.take_body()
.into_string()
.await
.unwrap(),
"hello sunli"
);
assert_eq!(
index
.call(Request::builder().uri(Uri::from_static("/")).finish())
.await
.unwrap()
.take_body()
.into_string()
.await
.unwrap(),
"hello"
);
Required methods
fn into_response(self) -> Response
fn into_response(self) -> Response
Consume itself and return 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_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");
fn 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");
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");