Responder

Trait Responder 

Source
pub trait Responder {
    type Body: MessageBody + 'static;

    // Required method
    fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>;

    // Provided method
    fn customize(self) -> CustomizeResponder<Self>
       where Self: Sized { ... }
}
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:

§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§

Source

type Body: MessageBody + 'static

Required Methods§

Source

fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>

Convert self to HttpResponse.

Provided Methods§

Source

fn customize(self) -> CustomizeResponder<Self>
where Self: Sized,

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§

Source§

impl Responder for &'static str

Source§

type Body = &'static str

Source§

fn respond_to( self, _: &HttpRequest, ) -> HttpResponse<<&'static str as Responder>::Body>

Source§

impl Responder for &'static [u8]

Source§

type Body = &'static [u8]

Source§

fn respond_to( self, _: &HttpRequest, ) -> HttpResponse<<&'static [u8] as Responder>::Body>

Source§

impl Responder for &String

Source§

impl Responder for Cow<'_, str>

Source§

impl Responder for String

Source§

impl Responder for Vec<u8>

Source§

impl Responder for ResponseBuilder

Source§

impl Responder for ByteString

Source§

impl<E> Responder for Result<(), E>
where E: Into<Error>,

Source§

impl<R> Responder for (R, StatusCode)
where R: Responder,

Source§

impl<R> Responder for Option<R>
where R: Responder,

Source§

impl<R, E> Responder for Result<R, E>
where R: Responder, E: Into<Error>,

Implementors§

Source§

impl Responder for Response<BoxBody>

Source§

impl Responder for HttpResponseBuilder

Source§

impl Responder for NamedFile

Source§

impl Responder for Bytes

Source§

impl Responder for BytesMut

Source§

impl Responder for Html

Source§

impl Responder for Redirect

Source§

impl<B> Responder for HttpResponse<B>
where B: MessageBody + 'static,

Source§

type Body = B

Source§

impl<L, R> Responder for Either<L, R>
where L: Responder, R: Responder,

See here for example of usage as a handler return type.

Source§

impl<T> Responder for InternalError<T>
where T: Debug + Display + 'static,

Source§

impl<T> Responder for CustomizeResponder<T>
where T: Responder,

Source§

impl<T> Responder for Form<T>
where T: Serialize,

See here for example of usage as a handler return type.

Source§

impl<T> Responder for Json<T>
where T: Serialize,

Creates response with OK status code, correct content type header, and serialized JSON payload.

If serialization failed