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) -> HttpResponseBuilder ⓘ
pub fn new(status: StatusCode) -> HttpResponseBuilder ⓘ
Create response builder
Sourcepub fn status(&mut self, status: StatusCode) -> &mut HttpResponseBuilder ⓘ
pub fn status(&mut self, status: StatusCode) -> &mut HttpResponseBuilder ⓘ
Set HTTP status code of this response.
Sourcepub fn insert_header(
&mut self,
header: impl TryIntoHeaderPair,
) -> &mut HttpResponseBuilder ⓘ
pub fn insert_header( &mut self, header: impl TryIntoHeaderPair, ) -> &mut HttpResponseBuilder ⓘ
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 HttpResponseBuilder ⓘ
pub fn append_header( &mut self, header: impl TryIntoHeaderPair, ) -> &mut HttpResponseBuilder ⓘ
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 HttpResponseBuilder ⓘ
pub fn reason(&mut self, reason: &'static str) -> &mut HttpResponseBuilder ⓘ
Set the custom reason for the response.
Sourcepub fn keep_alive(&mut self) -> &mut HttpResponseBuilder ⓘ
pub fn keep_alive(&mut self) -> &mut HttpResponseBuilder ⓘ
Set connection type to KeepAlive
Sourcepub fn upgrade<V>(&mut self, value: V) -> &mut HttpResponseBuilder ⓘwhere
V: TryIntoHeaderValue,
pub fn upgrade<V>(&mut self, value: V) -> &mut HttpResponseBuilder ⓘwhere
V: TryIntoHeaderValue,
Set connection type to Upgrade
Sourcepub fn force_close(&mut self) -> &mut HttpResponseBuilder ⓘ
pub fn force_close(&mut self) -> &mut HttpResponseBuilder ⓘ
Force close connection, even if it is marked as keep-alive
Sourcepub fn no_chunking(&mut self, len: u64) -> &mut HttpResponseBuilder ⓘ
pub fn no_chunking(&mut self, len: u64) -> &mut HttpResponseBuilder ⓘ
Disable chunked transfer encoding for HTTP/1.1 streaming responses.
Sourcepub fn content_type<V>(&mut self, value: V) -> &mut HttpResponseBuilder ⓘwhere
V: TryIntoHeaderValue,
pub fn content_type<V>(&mut self, value: V) -> &mut HttpResponseBuilder ⓘwhere
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) -> HttpResponsewhere
B: MessageBody + 'static,
pub fn body<B>(&mut self, body: B) -> HttpResponsewhere
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.
Sourcepub fn take(&mut self) -> HttpResponseBuilder ⓘ
pub fn take(&mut self) -> HttpResponseBuilder ⓘ
This method construct new HttpResponseBuilder
Trait Implementations§
Source§impl From<HttpResponseBuilder> for HttpResponse
impl From<HttpResponseBuilder> for HttpResponse
Source§fn from(builder: HttpResponseBuilder) -> HttpResponse
fn from(builder: HttpResponseBuilder) -> HttpResponse
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<<HttpResponseBuilder as Responder>::Body>
fn respond_to( self, _: &HttpRequest, ) -> HttpResponse<<HttpResponseBuilder as Responder>::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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
Source§fn map_into<U>(self) -> MapInto<Self, U> ⓘ
fn map_into<U>(self) -> MapInto<Self, U> ⓘ
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
f
. Read moreSource§fn left_future<B>(self) -> Either<Self, B> ⓘ
fn left_future<B>(self) -> Either<Self, B> ⓘ
Source§fn right_future<A>(self) -> Either<A, Self> ⓘ
fn right_future<A>(self) -> Either<A, Self> ⓘ
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self> ⓘ
fn flatten(self) -> Flatten<Self> ⓘ
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§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 moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
Source§fn catch_unwind(self) -> CatchUnwind<Self> ⓘwhere
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self> ⓘwhere
Self: Sized + UnwindSafe,
Source§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>>
Source§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,
Source§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 = ()
>.Source§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
>.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
Self: Sized,
fn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
Self: Sized,
CancellationToken::run_until_cancelled
,
but with the advantage that it is easier to write fluent call chains,
and biased towards waiting for CancellationToken
to complete. Read moreSource§fn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
Self: Sized,
fn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
Self: Sized,
CancellationToken::run_until_cancelled_owned
,
but with the advantage that it is easier to write fluent call chains,
and biased towards waiting for CancellationToken
to complete. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more