logo
pub trait Responder {
    type Body: MessageBody + 'static;
    fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body>;

    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.

Associated Types

Required methods

Convert self to HttpResponse.

Provided methods

Wraps responder to allow alteration of its response.

See CustomizeResponder docs for 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

Implementors

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

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

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

If serialization failed