pub struct CustomResponder<T: Responder<St>, St: State> { /* private fields */ }Expand description
Allows to override status code and headers for a responder.
Implementations§
Source§impl<T: Responder<St>, St: State> CustomResponder<T, St>
impl<T: Responder<St>, St: State> CustomResponder<T, St>
Sourcepub fn with_status(self, status: StatusCode) -> Self
pub fn with_status(self, status: StatusCode) -> Self
Override a status code for the Responder’s response.
The status replaces whatever the inner responder produced, including
error statuses rendered by a Result error. Use
result.map(|v| v.with_status(status)) to change only the success
status; see Responder::with_status().
use ntex::http::StatusCode;
use ntex::web::{HttpRequest, Responder};
fn index(req: HttpRequest) -> impl Responder {
"Welcome!".with_status(StatusCode::OK)
}Sourcepub fn set_header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn set_header<K, V>(self, key: K, value: V) -> Selfwhere
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 Responder’s response.
Replaces values with the same name, both those produced by the inner
responder and those added by earlier set_header() or
with_header() calls. An invalid name or value
makes the response 500 Internal Server Error.
use ntex::http::StatusCode;
use ntex::web::{HttpRequest, Responder};
fn index(req: HttpRequest) -> impl Responder {
"Created"
.with_status(StatusCode::CREATED)
.set_header("x-version", "1.2.3")
}Sourcepub fn with_header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn with_header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Adds a header value to the Responder’s response.
Keeps values added by earlier with_header() or
set_header() calls, so one name can carry
several values. Values with the same name produced by the inner
responder are still replaced. An invalid name or value makes the
response 500 Internal Server Error.
use ntex::web::{HttpRequest, Responder};
fn index(req: HttpRequest) -> impl Responder {
"Welcome!"
.with_header("vary", "accept")
.with_header("vary", "accept-encoding")
}