Trait uiuifree_actix_web_util::Responder
source · [−]pub trait Responder {
type Body: 'static + MessageBody;
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>;
fn customize(self) -> CustomizeResponder<Self> { ... }
}Expand description
Trait implemented by types that can be converted to an HTTP response.
Any types that implement this trait can be used in the return type of a handler. Since handlers
will only have one return type, it is idiomatic to use opaque return types -> impl Responder.
Implementations
It is often not required to implement Responder for your own types due to a broad base of
built-in implementations:
HttpResponseandHttpResponseBuilderOption<R>whereR: ResponderResult<R, E>whereR: ResponderandE: ResponseError(R, StatusCode) whereR: Responder`&'static str,String,&'_ String,Cow<'_, str>,ByteString&'static [u8],Vec<u8>,Bytes,BytesMutJson<T>andForm<T>whereT: SerializeEither<L, R>whereL: SerializeandR: SerializeCustomizeResponder<R>actix_files::NamedFile- Experimental responders from
actix-web-lab - Third party integrations may also have implemented
Responderwhere appropriate. For example, HTML templating engines.
Customizing Responder Output
Calling .customize() on any responder type will wrap it in a
CustomizeResponder capable of overriding various parts of the response such as the status
code and header map.
Required Associated Types
type Body: 'static + MessageBody
Required Methods
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>
Convert self to HttpResponse.
Provided Methods
fn customize(self) -> CustomizeResponder<Self>
fn customize(self) -> CustomizeResponder<Self>
Wraps responder to allow alteration of its response.
See CustomizeResponder docs for more details on its capabilities.
Examples
use actix_web::{Responder, http::StatusCode, test::TestRequest};
let responder = "Hello world!"
.customize()
.with_status(StatusCode::BAD_REQUEST)
.insert_header(("x-hello", "world"));
let request = TestRequest::default().to_http_request();
let response = responder.respond_to(&request);
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_eq!(response.headers().get("x-hello").unwrap(), "world");Implementations on Foreign Types
sourceimpl Responder for ResponseBuilder
impl Responder for ResponseBuilder
type Body = BoxBody
fn respond_to(
self,
req: &HttpRequest
) -> HttpResponse<<ResponseBuilder as Responder>::Body>
sourceimpl<R> Responder for (R, StatusCode) where
R: Responder,
impl<R> Responder for (R, StatusCode) where
R: Responder,
type Body = <R as Responder>::Body
fn respond_to(
self,
req: &HttpRequest
) -> HttpResponse<<(R, StatusCode) as Responder>::Body>
sourceimpl<R, E> Responder for Result<R, E> where
R: Responder,
E: Into<Error>,
impl<R, E> Responder for Result<R, E> where
R: Responder,
E: Into<Error>,
type Body = EitherBody<<R as Responder>::Body, BoxBody>
fn respond_to(
self,
req: &HttpRequest
) -> HttpResponse<<Result<R, E> as Responder>::Body>
sourceimpl Responder for String
impl Responder for String
type Body = String
fn respond_to(self, &HttpRequest) -> HttpResponse<<String as Responder>::Body>
sourceimpl Responder for ByteString
impl Responder for ByteString
type Body = ByteString
fn respond_to(
self,
&HttpRequest
) -> HttpResponse<<ByteString as Responder>::Body>
sourceimpl Responder for &String
impl Responder for &String
type Body = String
fn respond_to(self, &HttpRequest) -> HttpResponse<<&String as Responder>::Body>
sourceimpl Responder for Cow<'_, str>
impl Responder for Cow<'_, str>
type Body = String
fn respond_to(
self,
&HttpRequest
) -> HttpResponse<<Cow<'_, str> as Responder>::Body>
sourceimpl Responder for &'static str
impl Responder for &'static str
type Body = &'static str
fn respond_to(
self,
&HttpRequest
) -> HttpResponse<<&'static str as Responder>::Body>
sourceimpl Responder for Vec<u8, Global>
impl Responder for Vec<u8, Global>
type Body = Vec<u8, Global>
fn respond_to(
self,
&HttpRequest
) -> HttpResponse<<Vec<u8, Global> as Responder>::Body>
sourceimpl<R> Responder for Option<R> where
R: Responder,
impl<R> Responder for Option<R> where
R: Responder,
type Body = EitherBody<<R as Responder>::Body, BoxBody>
fn respond_to(
self,
req: &HttpRequest
) -> HttpResponse<<Option<R> as Responder>::Body>
sourceimpl Responder for &'static [u8]
impl Responder for &'static [u8]
type Body = &'static [u8]
fn respond_to(
self,
&HttpRequest
) -> HttpResponse<<&'static [u8] as Responder>::Body>
Implementors
sourceimpl Responder for HttpResponseBuilder
impl Responder for HttpResponseBuilder
sourceimpl<B> Responder for HttpResponse<B> where
B: 'static + MessageBody,
impl<B> Responder for HttpResponse<B> where
B: 'static + MessageBody,
type Body = B
sourceimpl<L, R> Responder for Either<L, R> where
L: Responder,
R: Responder,
impl<L, R> Responder for Either<L, R> where
L: Responder,
R: Responder,
See here for example of usage as a handler return type.