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.

Implementations§

Source§

impl ResponseBuilder

Source

pub fn new(status: StatusCode) -> Self

Create response builder

Source

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

Set HTTP status code of this response.

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

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

Set a header.

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

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

Set the custom reason for the response.

Source

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

Set connection type to KeepAlive

Source

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

Set connection type to Upgrade

Source

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

Force close connection, even if it is marked as keep-alive

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

Set response content type.

Source

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

Set content length.

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

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

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

Responses extensions.

Source

pub fn extensions_mut(&self) -> RefMut<'_, Extensions>

Mutable reference to a the response’s extensions.

Source

pub fn body<B: Into<Body>>(&mut self, body: B) -> Response

Set a body and generate Response.

ResponseBuilder can not be used after this call.

Source

pub fn message_body<B>(&mut self, body: B) -> Response<B>

Set a body and generate Response.

ResponseBuilder can not be used after this call.

Source

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

Set a streaming body and generate Response.

ResponseBuilder can not be used after this call.

Source

pub fn json<T: Serialize>(&mut self, value: &T) -> Response

Set a json body and generate Response.

ResponseBuilder can not be used after this call.

Source

pub fn finish(&mut self) -> Response

Set an empty body and generate Response.

ResponseBuilder can not be used after this call.

Source

pub fn take(&mut self) -> ResponseBuilder

This method construct new ResponseBuilder

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<Err: ErrorRenderer> Responder<Err> for ResponseBuilder

Source§

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

Convert itself to http response.
Source§

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

Override a status code for a Responder. Read more
Source§

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

Add header to the Responder’s response. 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> Same for T

Source§

type Output = T

Should always be Self
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.