Skip to main content

ResponseBuilder

Struct ResponseBuilder 

Source
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

Source

pub fn new(status: StatusCode) -> Self

Creates a response builder with the supplied status.

Source

pub fn status(&mut self, status: StatusCode) -> &mut Self

Sets the response status code.

Source

pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self

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

pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self

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

pub fn reason(&mut self, reason: &'static str) -> &mut Self

Sets a custom HTTP/1 reason phrase.

Source

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().

Source

pub fn upgrade<V>(&mut self, value: V) -> &mut Self

Marks the connection as upgraded and sets the Upgrade header.

Source

pub fn force_close(&mut self) -> &mut Self

Forces the connection to close after this response.

Source

pub fn no_chunking(&mut self) -> &mut Self

Disable chunked transfer encoding for HTTP/1.1 streaming responses.

Source

pub fn content_type<V>(&mut self, value: V) -> &mut Self

Sets the Content-Type header.

Source

pub fn content_length(&mut self, len: u64) -> &mut Self

Appends a Content-Length header.

Source

pub fn cookie<C>(&mut self, cookie: C) -> &mut Self
where C: Into<Cookie<'static>>,

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

pub fn extensions(&self) -> Ref<'_, Extensions>

Returns the response extensions.

§Panics

Panics if the builder has already produced or transferred a response.

Source

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.

Source

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.

Source

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.

Source

pub fn streaming<S, E>(&mut self, stream: S) -> Response
where S: Stream<Item = Result<Bytes, E>> + Unpin + 'static, E: Error + 'static,

Sets a streaming body and creates the response.

§Panics

Panics if the builder has already produced or transferred a response.

Source

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.

Source

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.

Source

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

Source§

fn get_encoding(&self) -> Option<ContentEncoding>

Get content encoding
Source§

fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self

Set content encoding
Source§

impl Debug for ResponseBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a ResponseHead> for ResponseBuilder

Convert ResponseHead to a ResponseBuilder

Source§

fn from(head: &'a ResponseHead) -> ResponseBuilder

Converts to this type from the input type.
Source§

impl<B> From<Response<B>> for ResponseBuilder

Convert Response to a ResponseBuilder. Body get dropped.

Source§

fn from(res: Response<B>) -> ResponseBuilder

Converts to this type from the input type.
Source§

impl From<ResponseBuilder> for Response

Source§

fn from(builder: ResponseBuilder) -> Self

Converts to this type from the input type.
Source§

impl<St: State> Responder<St> for ResponseBuilder

Source§

async fn respond_to(self, _: &St, _: &HttpRequest) -> Response

Builds an HTTP response from this value.
Source§

fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>
where Self: Sized,

Replaces the status code produced by this responder. Read more
Source§

fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>

Sets a header on the response produced by this responder. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.