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§
sourcefn respond_to(self, req: &HttpRequest) -> impl Future<Output = Response>
fn respond_to(self, req: &HttpRequest) -> impl Future<Output = Response>
Convert itself to http response.
Provided Methods§
sourcefn with_status(self, status: StatusCode) -> CustomResponder<Self, Err>where
Self: Sized,
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)
}
sourcefn 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>,
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>,
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
impl<'a, Err: ErrorRenderer> Responder<Err> for &'a String
async fn respond_to(self, _: &HttpRequest) -> Response
source§impl<Err: ErrorRenderer> Responder<Err> for &'static str
impl<Err: ErrorRenderer> Responder<Err> for &'static str
async fn respond_to(self, _: &HttpRequest) -> Response
source§impl<Err: ErrorRenderer> Responder<Err> for &'static [u8]
impl<Err: ErrorRenderer> Responder<Err> for &'static [u8]
async fn respond_to(self, _: &HttpRequest) -> Response
source§impl<Err: ErrorRenderer> Responder<Err> for String
impl<Err: ErrorRenderer> Responder<Err> for String
async fn respond_to(self, _: &HttpRequest) -> Response
source§impl<T, E, Err> Responder<Err> for Result<T, E>
impl<T, E, Err> Responder<Err> for Result<T, E>
async fn respond_to(self, req: &HttpRequest) -> Response
source§impl<T, Err> Responder<Err> for (T, StatusCode)where
T: Responder<Err>,
Err: ErrorRenderer,
impl<T, Err> Responder<Err> for (T, StatusCode)where
T: Responder<Err>,
Err: ErrorRenderer,
async fn respond_to(self, req: &HttpRequest) -> Response
source§impl<T, Err> Responder<Err> for Option<T>where
T: Responder<Err>,
Err: ErrorRenderer,
impl<T, Err> Responder<Err> for Option<T>where
T: Responder<Err>,
Err: ErrorRenderer,
async fn respond_to(self, req: &HttpRequest) -> Response
Implementors§
impl<A, B, Err> Responder<Err> for Either<A, B>
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!")
}
}