Trait actix_web::Responder[][src]

pub trait Responder {
    fn respond_to(self, req: &HttpRequest) -> HttpResponse
Notable traits for HttpResponse<Body>
impl Future for HttpResponse<Body> type Output = Result<Response<Body>, Error>;
; fn with_status(self, status: StatusCode) -> CustomResponder<Self>
    where
        Self: Sized
, { ... }
fn with_header<H>(self, header: H) -> CustomResponder<Self>
    where
        Self: Sized,
        H: IntoHeaderPair
, { ... } }
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.

Required methods

Convert self to HttpResponse.

Provided methods

Override a status code for a Responder.

use actix_web::{http::StatusCode, HttpRequest, Responder};

fn index(req: HttpRequest) -> impl Responder {
    "Welcome!".with_status(StatusCode::OK)
}

Insert header to the final response.

Overrides other headers with the same name.

use actix_web::{web, HttpRequest, Responder};
use serde::Serialize;

#[derive(Serialize)]
struct MyObj {
    name: String,
}

fn index(req: HttpRequest) -> impl Responder {
    web::Json(MyObj { name: "Name".to_owned() })
        .with_header(("x-version", "1.2.3"))
}

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