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.
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
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> 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 remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
() on completion and sends
its output to another future on a separate task. Read moreSource§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>.