pub struct ResponseBuilder { /* private fields */ }Expand description
An HTTP response builder
This type can be used to construct an instance of Response through a
builder-like pattern.
Header conversion errors are stored by the builder. A later call to
ResponseBuilder::body, ResponseBuilder::message_body,
ResponseBuilder::streaming, ResponseBuilder::json, or
ResponseBuilder::build converts the stored error into an error response.
After an error is stored, subsequent configuration does not affect the
returned response.
Implementations§
Source§impl ResponseBuilder
impl ResponseBuilder
Sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
Creates a response builder with the supplied status.
Sourcepub fn status(&mut self, status: StatusCode) -> &mut Self
pub fn status(&mut self, status: StatusCode) -> &mut Self
Sets the response status code.
Sourcepub fn header<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Append a header to existing headers.
use ntex::http::{header, Request, Response};
fn index(req: Request) -> Response {
Response::Ok()
.header("X-TEST", "value")
.header(header::CONTENT_TYPE, "application/json")
.build()
}Sourcepub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut 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>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Sets a header, replacing any existing values with the same name.
use ntex::http::{header, Request, Response};
fn index(req: Request) -> Response {
Response::Ok()
.set_header("X-TEST", "value")
.set_header(header::CONTENT_TYPE, "application/json")
.build()
}Sourcepub fn reason(&mut self, reason: &'static str) -> &mut Self
pub fn reason(&mut self, reason: &'static str) -> &mut Self
Sets a custom HTTP/1 reason phrase.
Sourcepub fn keep_alive(&mut self) -> &mut Self
pub fn keep_alive(&mut self) -> &mut Self
Marks the response connection as persistent.
Has no effect if the response connection is already marked to be
closed, see ResponseHead::set_connection_type().
Sourcepub fn upgrade<V>(&mut self, value: V) -> &mut Self
pub fn upgrade<V>(&mut self, value: V) -> &mut Self
Marks the connection as upgraded and sets the Upgrade header.
Sourcepub fn force_close(&mut self) -> &mut Self
pub fn force_close(&mut self) -> &mut Self
Forces the connection to close after this response.
Sourcepub fn no_chunking(&mut self) -> &mut Self
pub fn no_chunking(&mut self) -> &mut Self
Disable chunked transfer encoding for HTTP/1.1 streaming responses.
Sourcepub fn content_type<V>(&mut self, value: V) -> &mut Self
pub fn content_type<V>(&mut self, value: V) -> &mut Self
Sets the Content-Type header.
Sourcepub fn content_length(&mut self, len: u64) -> &mut Self
pub fn content_length(&mut self, len: u64) -> &mut Self
Appends a Content-Length header.
Set a cookie.
use coo_kie as cookie;
use ntex::http::{Request, Response};
fn index(req: Request) -> Response {
Response::Ok()
.cookie(
cookie::Cookie::build(("name", "value"))
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true)
)
.build()
}Remove cookie.
use ntex::http::{Request, Response, HttpMessage};
fn index(req: Request) -> Response {
let mut builder = Response::Ok();
if let Some(ref cookie) = req.cookie("name") {
builder.del_cookie(cookie);
}
builder.build()
}Sourcepub fn extensions(&self) -> Ref<'_, Extensions>
pub fn extensions(&self) -> Ref<'_, Extensions>
Returns the response extensions.
§Panics
Panics if the builder has already produced or transferred a response.
Sourcepub fn extensions_mut(&self) -> RefMut<'_, Extensions>
pub fn extensions_mut(&self) -> RefMut<'_, Extensions>
Returns mutable access to the response extensions.
§Panics
Panics if the builder has already produced or transferred a response.
Sourcepub fn body<B: Into<Body>>(&mut self, body: B) -> Response
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response
Sets a body and creates the response.
§Panics
Panics if the builder has already produced or transferred a response.
Sourcepub fn message_body<B>(&mut self, body: B) -> Response<B>
pub fn message_body<B>(&mut self, body: B) -> Response<B>
Sets a typed body and creates the response.
§Panics
Panics if the builder has already produced or transferred a response.
Sourcepub fn streaming<S, E>(&mut self, stream: S) -> Response
pub fn streaming<S, E>(&mut self, stream: S) -> Response
Sets a streaming body and creates the response.
§Panics
Panics if the builder has already produced or transferred a response.
Sourcepub fn json<T: Serialize>(&mut self, value: &T) -> Response
pub fn json<T: Serialize>(&mut self, value: &T) -> Response
Serializes value as JSON and creates the response.
Sets Content-Type: application/json unless a content type is already
present. If serialization fails, the serialization error is converted
into an HTTP error response.
§Panics
Panics if serialization succeeds after the builder has already produced or transferred a response.
Sourcepub fn build(&mut self) -> Response
pub fn build(&mut self) -> Response
Creates the response with an empty body.
§Panics
Panics if the builder has already produced or transferred a response.
Sourcepub fn take(&mut self) -> ResponseBuilder
pub fn take(&mut self) -> ResponseBuilder
Transfers the builder state into a new builder.
The current builder is left consumed and cannot produce a response.
Trait Implementations§
Source§impl BodyEncoding for HttpResponseBuilder
impl BodyEncoding for HttpResponseBuilder
Source§fn get_encoding(&self) -> Option<ContentEncoding>
fn get_encoding(&self) -> Option<ContentEncoding>
Source§fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self
Source§impl Debug for ResponseBuilder
impl Debug for ResponseBuilder
Source§impl<'a> From<&'a ResponseHead> for ResponseBuilder
Convert ResponseHead to a ResponseBuilder
impl<'a> From<&'a ResponseHead> for ResponseBuilder
Convert ResponseHead to a ResponseBuilder
Source§fn from(head: &'a ResponseHead) -> ResponseBuilder
fn from(head: &'a ResponseHead) -> ResponseBuilder
Source§impl<B> From<Response<B>> for ResponseBuilder
Convert Response to a ResponseBuilder. Body get dropped.
impl<B> From<Response<B>> for ResponseBuilder
Convert Response to a ResponseBuilder. Body get dropped.