Trait ntex::web::Responder[][src]

pub trait Responder<Err = DefaultError> {
    type Error;
    type Future: Future<Output = Response>;
    fn respond_to(self, req: &HttpRequest) -> Self::Future;

    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.

Associated Types

The associated error which can be returned.

The future response value.

Required methods

Convert itself to AsyncResult or Error.

Provided methods

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)
}

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")
}

Implementations on Foreign Types

Implementors

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!")
    }
}