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§
source§impl 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 Selfwhere
V: TryIntoHeaderValue,
pub fn upgrade<V>(&mut self, value: V) -> &mut Selfwhere 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 Selfwhere
V: TryIntoHeaderValue,
pub fn content_type<V>(&mut self, value: V) -> &mut Selfwhere V: TryIntoHeaderValue,
Set response content type.
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§
source§impl Debug for ResponseBuilder
impl Debug for ResponseBuilder
source§impl Default for ResponseBuilder
impl Default for ResponseBuilder
source§impl<'a> From<&'a ResponseHead> for 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
impl<B> From<Response<B>> for ResponseBuilder
Convert Response
to a ResponseBuilder
. Body get dropped.