pub struct ResponseBuilder { /* private fields */ }
Expand description

An HTTP response builder.

Used to construct an instance of Response using a builder pattern. Response builders are often created using Response::build.

Examples

use actix_http::{Response, ResponseBuilder, StatusCode, body, header};

let mut res: Response<_> = Response::build(StatusCode::OK)
    .content_type(mime::APPLICATION_JSON)
    .insert_header((header::SERVER, "my-app/1.0"))
    .append_header((header::SET_COOKIE, "a=1"))
    .append_header((header::SET_COOKIE, "b=2"))
    .body("1234");

assert_eq!(res.status(), StatusCode::OK);

assert!(res.headers().contains_key("server"));
assert_eq!(res.headers().get_all("set-cookie").count(), 2);

assert_eq!(body::to_bytes(res.into_body()).await.unwrap(), &b"1234"[..]);

Implementations§

source§

impl ResponseBuilder

source

pub fn new(status: StatusCode) -> Self

Create response builder

Examples
use actix_http::{Response, ResponseBuilder, StatusCode};
let res: Response<_> = ResponseBuilder::default().finish();
assert_eq!(res.status(), StatusCode::OK);
source

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

Set HTTP status code of this response.

Examples
use actix_http::{ResponseBuilder, StatusCode};
let res = ResponseBuilder::default().status(StatusCode::NOT_FOUND).finish();
assert_eq!(res.status(), StatusCode::NOT_FOUND);
source

pub fn insert_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self

Insert a header, replacing any that were set with an equivalent field name.

Examples
use actix_http::{ResponseBuilder, header};

let res = ResponseBuilder::default()
    .insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON))
    .insert_header(("X-TEST", "value"))
    .finish();

assert!(res.headers().contains_key("content-type"));
assert!(res.headers().contains_key("x-test"));
source

pub fn append_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self

Append a header, keeping any that were set with an equivalent field name.

Examples
use actix_http::{ResponseBuilder, header};

let res = ResponseBuilder::default()
    .append_header((header::CONTENT_TYPE, mime::APPLICATION_JSON))
    .append_header(("X-TEST", "value1"))
    .append_header(("X-TEST", "value2"))
    .finish();

assert_eq!(res.headers().get_all("content-type").count(), 1);
assert_eq!(res.headers().get_all("x-test").count(), 2);
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 Selfwhere V: TryIntoHeaderValue,

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, len: u64) -> &mut Self

Disable chunked transfer encoding for HTTP/1.1 streaming responses.

source

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

Set response content type.

source

pub fn body<B>(&mut self, body: B) -> Response<EitherBody<B>>where B: MessageBody + 'static,

Generate response with a wrapped body.

This ResponseBuilder will be left in a useless state.

source

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

Generate response with a body.

This ResponseBuilder will be left in a useless state.

source

pub fn finish(&mut self) -> Response<EitherBody<()>>

Generate response with an empty body.

This ResponseBuilder will be left in a useless state.

source

pub fn take(&mut self) -> ResponseBuilder

Create an owned ResponseBuilder, leaving the original in a useless state.

Trait Implementations§

source§

impl Debug for ResponseBuilder

source§

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

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

impl Default for ResponseBuilder

source§

fn default() -> Self

Returns the “default value” for a type. 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<EitherBody<()>>

source§

fn from(builder: ResponseBuilder) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more