Struct actix_web::HttpResponseBuilder
source · [−]pub struct HttpResponseBuilder { /* 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
sourceimpl HttpResponseBuilder
impl HttpResponseBuilder
sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
Create response builder
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.
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.
use actix_web::{HttpResponse, http::header};
HttpResponse::Ok()
.insert_header(header::ContentType(mime::APPLICATION_JSON))
.insert_header(("X-TEST", "value"))
.finish();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.
use actix_web::{HttpResponse, http::header};
HttpResponse::Ok()
.append_header(header::ContentType(mime::APPLICATION_JSON))
.append_header(("X-TEST", "value1"))
.append_header(("X-TEST", "value2"))
.finish();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.
Add a cookie to the response.
To send a “removal” cookie, call .make_removal() on the
given cookie. See HttpResponse::add_removal_cookie() to learn more.
Examples
Send a new cookie:
use actix_web::{HttpResponse, cookie::Cookie};
let res = HttpResponse::Ok()
.cookie(
Cookie::build("name", "value")
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true)
.finish(),
)
.finish();Send a removal cookie:
use actix_web::{HttpResponse, cookie::Cookie};
// the name, domain and path match the cookie created in the previous example
let mut cookie = Cookie::build("name", "value-does-not-matter")
.domain("www.rust-lang.org")
.path("/")
.finish();
cookie.make_removal();
let res = HttpResponse::Ok()
.cookie(cookie)
.finish();sourcepub fn extensions(&self) -> Ref<'_, Extensions>
pub fn extensions(&self) -> Ref<'_, Extensions>
Returns a reference to the response-local data/extensions container.
sourcepub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>
Returns a mutable reference to the response-local data/extensions container.
sourcepub fn body<B>(&mut self, body: B) -> HttpResponse<BoxBody>where
B: MessageBody + 'static,
pub fn body<B>(&mut self, body: B) -> HttpResponse<BoxBody>where
B: MessageBody + 'static,
Set a body and build the HttpResponse.
Unlike message_body, errors are converted into error
responses immediately.
HttpResponseBuilder can not be used after this call.
sourcepub fn message_body<B>(&mut self, body: B) -> Result<HttpResponse<B>, Error>
pub fn message_body<B>(&mut self, body: B) -> Result<HttpResponse<B>, Error>
Set a body and build the HttpResponse.
HttpResponseBuilder can not be used after this call.
sourcepub fn streaming<S, E>(&mut self, stream: S) -> HttpResponsewhere
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<Box<dyn Error>> + 'static,
pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponsewhere
S: Stream<Item = Result<Bytes, E>> + 'static,
E: Into<Box<dyn Error>> + 'static,
Set a streaming body and build the HttpResponse.
HttpResponseBuilder can not be used after this call.
sourcepub fn json(&mut self, value: impl Serialize) -> HttpResponse
pub fn json(&mut self, value: impl Serialize) -> HttpResponse
Set a JSON body and build the HttpResponse.
HttpResponseBuilder can not be used after this call.
sourcepub fn finish(&mut self) -> HttpResponse
pub fn finish(&mut self) -> HttpResponse
Set an empty body and build the HttpResponse.
HttpResponseBuilder can not be used after this call.
Trait Implementations
sourceimpl From<HttpResponseBuilder> for HttpResponse
impl From<HttpResponseBuilder> for HttpResponse
sourcefn from(builder: HttpResponseBuilder) -> Self
fn from(builder: HttpResponseBuilder) -> Self
sourceimpl From<HttpResponseBuilder> for Response<BoxBody>
impl From<HttpResponseBuilder> for Response<BoxBody>
sourcefn from(builder: HttpResponseBuilder) -> Self
fn from(builder: HttpResponseBuilder) -> Self
sourceimpl Future for HttpResponseBuilder
impl Future for HttpResponseBuilder
type Output = Result<HttpResponse<BoxBody>, Error>
type Output = Result<HttpResponse<BoxBody>, Error>
sourceimpl Responder for HttpResponseBuilder
impl Responder for HttpResponseBuilder
type Body = BoxBody
sourcefn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body>
HttpResponse.sourcefn customize(self) -> CustomizeResponder<Self>where
Self: Sized,
fn customize(self) -> CustomizeResponder<Self>where
Self: Sized,
Auto Trait Implementations
impl !RefUnwindSafe for HttpResponseBuilder
impl !Send for HttpResponseBuilder
impl !Sync for HttpResponseBuilder
impl Unpin for HttpResponseBuilder
impl !UnwindSafe for HttpResponseBuilder
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> FutureExt for Twhere
T: Future + ?Sized,
impl<T> FutureExt for Twhere
T: Future + ?Sized,
sourcefn map<U, F>(self, f: F) -> Map<Self, F>where
F: FnOnce(Self::Output) -> U,
Self: Sized,
fn map<U, F>(self, f: F) -> Map<Self, F>where
F: FnOnce(Self::Output) -> U,
Self: Sized,
sourcefn map_into<U>(self) -> MapInto<Self, U>where
Self::Output: Into<U>,
Self: Sized,
fn map_into<U>(self) -> MapInto<Self, U>where
Self::Output: Into<U>,
Self: Sized,
sourcefn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
Self: Sized,
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
Self: Sized,
f. Read moresourcefn left_future<B>(self) -> Either<Self, B>where
B: Future<Output = Self::Output>,
Self: Sized,
fn left_future<B>(self) -> Either<Self, B>where
B: Future<Output = Self::Output>,
Self: Sized,
sourcefn right_future<A>(self) -> Either<A, Self>where
A: Future<Output = Self::Output>,
Self: Sized,
fn right_future<A>(self) -> Either<A, Self>where
A: Future<Output = Self::Output>,
Self: Sized,
sourcefn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
sourcefn flatten(self) -> Flatten<Self>where
Self::Output: Future,
Self: Sized,
fn flatten(self) -> Flatten<Self>where
Self::Output: Future,
Self: Sized,
sourcefn flatten_stream(self) -> FlattenStream<Self>where
Self::Output: Stream,
Self: Sized,
fn flatten_stream(self) -> FlattenStream<Self>where
Self::Output: Stream,
Self: Sized,
sourcefn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read more