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§
source§impl 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.
Available on crate feature cookies
only.
cookies
only.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) -> HttpResponse
pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponse
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§
source§impl From<HttpResponseBuilder> for HttpResponse
impl From<HttpResponseBuilder> for HttpResponse
source§fn from(builder: HttpResponseBuilder) -> Self
fn from(builder: HttpResponseBuilder) -> Self
source§impl From<HttpResponseBuilder> for Response<BoxBody>
impl From<HttpResponseBuilder> for Response<BoxBody>
source§fn from(builder: HttpResponseBuilder) -> Self
fn from(builder: HttpResponseBuilder) -> Self
source§impl Future for HttpResponseBuilder
impl Future for HttpResponseBuilder
source§impl Responder for HttpResponseBuilder
impl Responder for HttpResponseBuilder
type Body = BoxBody
source§fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body>
HttpResponse
.Auto Trait Implementations§
impl !Freeze for HttpResponseBuilder
impl !RefUnwindSafe for HttpResponseBuilder
impl !Send for HttpResponseBuilder
impl !Sync for HttpResponseBuilder
impl Unpin for HttpResponseBuilder
impl !UnwindSafe for HttpResponseBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f
. Read more§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
§fn 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§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
std
only.std
only.§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
alloc
only.§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
alloc
only.§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
§type IntoFuture = F
type IntoFuture = F
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
§impl<F, T, E> TryFuture for F
impl<F, T, E> TryFuture for F
§impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
§fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
sink
only.Sink
]. Read more