Trait Responder

Source
pub trait Responder<Err = DefaultError> {
    // Required method
    fn respond_to(self, req: &HttpRequest) -> impl Future<Output = Response>;

    // Provided methods
    fn with_status(self, status: StatusCode) -> CustomResponder<Self, Err>
       where Self: Sized { ... }
    fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, Err>
       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

Trait implemented by types that can be converted to a http response.

Types that implement this trait can be used as the return type of a handler.

Required Methods§

Source

fn respond_to(self, req: &HttpRequest) -> impl Future<Output = Response>

Convert itself to http response.

Provided Methods§

Source

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

Override a status code for a Responder.

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, Err>

Add header to the Responder’s response.

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", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<Err: ErrorRenderer> Responder<Err> for &'static str

Source§

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

Source§

impl<Err: ErrorRenderer> Responder<Err> for &'static [u8]

Source§

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

Source§

impl<Err: ErrorRenderer> Responder<Err> for &String

Source§

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

Source§

impl<Err: ErrorRenderer> Responder<Err> for String

Source§

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

Source§

impl<T, E, Err> Responder<Err> for Result<T, E>
where T: Responder<Err>, E: Into<Err::Container>, Err: ErrorRenderer,

Source§

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

Source§

impl<T, Err> Responder<Err> for (T, StatusCode)
where T: Responder<Err>, Err: ErrorRenderer,

Source§

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

Source§

impl<T, Err> Responder<Err> for Option<T>
where T: Responder<Err>, Err: ErrorRenderer,

Source§

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

Implementors§

Source§

impl<A, B, Err> Responder<Err> for Either<A, B>
where A: Responder<Err>, B: Responder<Err>, Err: ErrorRenderer,

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<Err: ErrorRenderer> Responder<Err> for Response

Source§

impl<Err: ErrorRenderer> Responder<Err> for ResponseBuilder

Source§

impl<Err: ErrorRenderer> Responder<Err> for Bytes

Source§

impl<Err: ErrorRenderer> Responder<Err> for BytesMut

Source§

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

Source§

impl<T: Serialize, Err: ErrorRenderer> Responder<Err> for Form<T>
where Err::Container: From<Error>,

Source§

impl<T: Serialize, Err: ErrorRenderer> Responder<Err> for Json<T>
where Err::Container: From<JsonError>,