Skip to main content

Responder

Trait Responder 

Source
pub trait Responder<St: State = ()> {
    // Required method
    async fn respond_to(self, st: &St, req: &HttpRequest) -> Response;

    // Provided methods
    fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>
       where Self: Sized { ... }
    fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>
       where Self: Sized,
             HeaderName: TryFrom<K>,
             HeaderValue: TryFrom<V>,
             <HeaderName as TryFrom<K>>::Error: Into<HttpError>,
             <HeaderValue as TryFrom<V>>::Error: Into<HttpError> { ... }
}
Expand description

Turns a handler’s return value into an HTTP response.

A handler can return any type that implements Responder. ntex provides implementations for common values such as responses, strings, byte buffers, Json, Form, Option<T>, and Result<T, E>.

After the handler finishes, ntex calls respond_to() with the application state and the original request. Custom responders can use them to choose the status, headers, or response body. The conversion is asynchronous, so it can also perform async work when needed.

For small changes, use with_status() or with_header() instead of implementing a new responder.

§Example

An application type can be returned directly from a handler after it implements Responder:

use ntex::http::Response;
use ntex::web::{self, HttpRequest, Responder};

struct Greeting(&'static str);

impl<St: web::State> Responder<St> for Greeting {
    async fn respond_to(self, _: &St, _: &HttpRequest) -> Response {
        Response::Ok()
            .content_type("text/plain; charset=utf-8")
            .body(self.0)
    }
}

async fn hello() -> Greeting {
    Greeting("Hello!")
}

let app = web::App::default().route("/", web::get().to(hello));

Required Methods§

Source

async fn respond_to(self, st: &St, req: &HttpRequest) -> Response

Builds an HTTP response from this value.

Provided Methods§

Source

fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>
where Self: Sized,

Replaces the status code produced by this responder.

The response body and headers are left unchanged.

use ntex::http::StatusCode;
use ntex::web::{HttpRequest, Responder};

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

fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>

Sets a header on the response produced by this responder.

An existing value with the same header name is replaced.

use ntex::web::{self, HttpRequest, Responder};
use serde::Serialize;

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

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Responder for ()

Source§

async fn respond_to(self, __arg1: &(), _: &HttpRequest) -> Response

Source§

impl<St, T, E> Responder<St> for Result<T, E>
where St: State, T: Responder<St>, E: WebResponseError<St, St::Error>,

Source§

async fn respond_to(self, st: &St, req: &HttpRequest) -> Response

Source§

impl<St, T> Responder<St> for (T, StatusCode)
where St: State, T: Responder<St>,

Source§

async fn respond_to(self, st: &St, req: &HttpRequest) -> Response

Source§

impl<St: State> Responder<St> for &'static [u8]

Source§

async fn respond_to(self, _: &St, _: &HttpRequest) -> Response

Source§

impl<St: State> Responder<St> for &'static str

Source§

async fn respond_to(self, _: &St, _: &HttpRequest) -> Response

Source§

impl<St: State> Responder<St> for &String

Source§

async fn respond_to(self, _: &St, _: &HttpRequest) -> Response

Source§

impl<St: State> Responder<St> for String

Source§

async fn respond_to(self, _: &St, _: &HttpRequest) -> Response

Source§

impl<T, St> Responder<St> for Option<T>
where T: Responder<St>, St: State,

Source§

async fn respond_to(self, st: &St, req: &HttpRequest) -> Response

Implementors§

Source§

impl<St, A, B> Responder<St> for Either<A, B>
where St: State, A: Responder<St>, B: Responder<St>,

Combines two different responder types into a single type

use ntex::{web::HttpResponse, util::Either};

fn index() -> Either<HttpResponse, &'static str> {
    if is_a_variant() {
        // <- choose left variant
        Either::Left(HttpResponse::BadRequest().body("Bad data"))
    } else {
        // <- Right variant
        Either::Right("Hello!")
    }
}
Source§

impl<St, T: Serialize> Responder<St> for Form<T>
where St: State, Error: WebResponseError<St, St::Error>,

Source§

impl<St, T: Serialize> Responder<St> for Json<T>
where St: State, JsonError: WebResponseError<St, St::Error>,

Source§

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

Source§

impl<St: State> Responder<St> for Bytes

Source§

impl<St: State> Responder<St> for BytesMut

Source§

impl<St: State> Responder<St> for Response

Source§

impl<St: State> Responder<St> for ResponseBuilder