Trait ntex::web::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")
}

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a, Err: ErrorRenderer> Responder<Err> for &'a String

source§

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

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