Struct actix_http::ResponseBuilder
source · [−]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
sourceimpl ResponseBuilder
impl ResponseBuilder
sourcepub fn new(status: StatusCode) -> Self
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);
sourcepub fn status(&mut self, status: StatusCode) -> &mut Self
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);
sourcepub fn insert_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self
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"));
sourcepub fn append_header(&mut self, header: impl TryIntoHeaderPair) -> &mut Self
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);
sourcepub fn reason(&mut self, reason: &'static str) -> &mut Self
pub fn reason(&mut self, reason: &'static str) -> &mut Self
Set the custom reason for the response.
sourcepub fn keep_alive(&mut self) -> &mut Self
pub fn keep_alive(&mut self) -> &mut Self
Set connection type to KeepAlive
sourcepub fn upgrade<V>(&mut self, value: V) -> &mut Self where
V: TryIntoHeaderValue,
pub fn upgrade<V>(&mut self, value: V) -> &mut Self where
V: TryIntoHeaderValue,
Set connection type to Upgrade
sourcepub fn force_close(&mut self) -> &mut Self
pub fn force_close(&mut self) -> &mut Self
Force close connection, even if it is marked as keep-alive
sourcepub fn no_chunking(&mut self, len: u64) -> &mut Self
pub fn no_chunking(&mut self, len: u64) -> &mut Self
Disable chunked transfer encoding for HTTP/1.1 streaming responses.
sourcepub fn content_type<V>(&mut self, value: V) -> &mut Self where
V: TryIntoHeaderValue,
pub fn content_type<V>(&mut self, value: V) -> &mut Self where
V: TryIntoHeaderValue,
Set response content type.
sourcepub fn extensions(&self) -> Ref<'_, Extensions>
pub fn extensions(&self) -> Ref<'_, Extensions>
Responses extensions
sourcepub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>
Mutable reference to a the response’s extensions
sourcepub fn body<B>(&mut self, body: B) -> Response<EitherBody<B>> where
B: MessageBody + 'static,
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.
sourcepub fn message_body<B>(&mut self, body: B) -> Result<Response<B>, Error>
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.
sourcepub fn finish(&mut self) -> Response<EitherBody<()>>
pub fn finish(&mut self) -> Response<EitherBody<()>>
Generate response with an empty body.
This ResponseBuilder
will be left in a useless state.
sourcepub fn take(&mut self) -> ResponseBuilder
pub fn take(&mut self) -> ResponseBuilder
Create an owned ResponseBuilder
, leaving the original in a useless state.
Trait Implementations
sourceimpl Debug for ResponseBuilder
impl Debug for ResponseBuilder
sourceimpl Default for ResponseBuilder
impl Default for ResponseBuilder
sourceimpl<'a> From<&'a ResponseHead> for ResponseBuilder
impl<'a> From<&'a ResponseHead> for ResponseBuilder
Convert ResponseHead
to a ResponseBuilder
sourcefn from(head: &'a ResponseHead) -> ResponseBuilder
fn from(head: &'a ResponseHead) -> ResponseBuilder
Converts to this type from the input type.
sourceimpl<B> From<Response<B>> for ResponseBuilder
impl<B> From<Response<B>> for ResponseBuilder
Convert Response
to a ResponseBuilder
. Body get dropped.
sourcefn from(res: Response<B>) -> ResponseBuilder
fn from(res: Response<B>) -> ResponseBuilder
Converts to this type from the input type.
sourceimpl From<ResponseBuilder> for Response<BoxBody>
impl From<ResponseBuilder> for Response<BoxBody>
sourcefn from(builder: ResponseBuilder) -> Self
fn from(builder: ResponseBuilder) -> Self
Converts to this type from the input type.
Auto Trait Implementations
impl !RefUnwindSafe for ResponseBuilder
impl !Send for ResponseBuilder
impl !Sync for ResponseBuilder
impl Unpin for ResponseBuilder
impl !UnwindSafe for ResponseBuilder
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more