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§
Sourceasync fn respond_to(self, st: &St, req: &HttpRequest) -> Response
async fn respond_to(self, st: &St, req: &HttpRequest) -> Response
Builds an HTTP response from this value.
Provided Methods§
Sourcefn with_status(self, status: StatusCode) -> CustomResponder<Self, St>where
Self: Sized,
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)
}Sourcefn 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>,
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>,
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 ()
impl Responder for ()
async fn respond_to(self, __arg1: &(), _: &HttpRequest) -> Response
Source§impl<St, T, E> Responder<St> for Result<T, E>
impl<St, T, E> Responder<St> for Result<T, E>
async fn respond_to(self, st: &St, req: &HttpRequest) -> Response
Source§impl<St, T> Responder<St> for (T, StatusCode)
impl<St, T> Responder<St> for (T, StatusCode)
async fn respond_to(self, st: &St, req: &HttpRequest) -> Response
Source§impl<St: State> Responder<St> for &'static [u8]
impl<St: State> Responder<St> for &'static [u8]
async fn respond_to(self, _: &St, _: &HttpRequest) -> Response
Source§impl<St: State> Responder<St> for &'static str
impl<St: State> Responder<St> for &'static str
async fn respond_to(self, _: &St, _: &HttpRequest) -> Response
Source§impl<St: State> Responder<St> for &String
impl<St: State> Responder<St> for &String
async fn respond_to(self, _: &St, _: &HttpRequest) -> Response
Source§impl<St: State> Responder<St> for String
impl<St: State> Responder<St> for String
async fn respond_to(self, _: &St, _: &HttpRequest) -> Response
Source§impl<T, St> Responder<St> for Option<T>
impl<T, St> Responder<St> for Option<T>
async fn respond_to(self, st: &St, req: &HttpRequest) -> Response
Implementors§
impl<St, A, B> Responder<St> 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!")
}
}