# actix_web::dev::HttpResponseBuilder - Rust
API documentation for the Rust `HttpResponseBuilder` struct in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, HttpResponseBuilder

# [−] [src] Struct actix_web::dev::HttpResponseBuilder

    pub struct HttpResponseBuilder { /* fields omitted */ }

An HTTP response builder

This type can be used to construct an instance of `HttpResponse` through a builder-like pattern.

## Methods

###

`impl HttpResponseBuilder`

[src]

####

`pub fn status(&mut self, status: StatusCode) -> &mut Self`

[src]

Set HTTP status code of this response.

####

`pub fn version(&mut self, version: Version) -> &mut Self`

[src]

Set HTTP version of this response.

By default response's http version depends on request's version.

####

`pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self where HeaderName: HttpTryFrom<K>, V: IntoHeaderValue,`

[src]

Set a header.

    use actix_web::{http, HttpRequest, HttpResponse};

    fn index(req: HttpRequest) -> HttpResponse {
        HttpResponse::Ok()
            .header("X-TEST", "value")
            .header(http::header::CONTENT_TYPE, "application/json")
            .finish()
    }
    fn main() {}

####

`pub fn reason(&mut self, reason: &'static str) -> &mut Self`

[src]

Set the custom reason for the response.

####

`pub fn content_encoding(&mut self, enc: ContentEncoding) -> &mut Self`

[src]

Set content encoding.

By default `ContentEncoding::Auto` is used, which automatically negotiates content encoding based on request's `Accept-Encoding` headers. To enforce specific encoding, use specific ContentEncoding` value.

####

`pub fn force_close(&mut self) -> &mut Self`

[src]

Force close connection, even if it is marked as keep-alive

####

`pub fn chunked(&mut self) -> &mut Self`

[src]

Enables automatic chunked transfer encoding

####

`pub fn no_chunking(&mut self) -> &mut Self`

[src]

Force disable chunked encoding

####

`pub fn content_type<V>(&mut self, value: V) -> &mut Self where HeaderValue: HttpTryFrom<V>,`

[src]

Set response content type

####

`pub fn content_length(&mut self, len: u64) -> &mut Self`

[src]

Set content length

####

`pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self`

[src]

Set a cookie

    use actix_web::{http, HttpRequest, HttpResponse, Result};

    fn index(req: HttpRequest) -> HttpResponse {
        HttpResponse::Ok()
            .cookie(
                http::Cookie::build("name", "value")
                    .domain("www.rust-lang.org")
                    .path("/")
                    .secure(true)
                    .http_only(true)
                    .finish(),
            )
            .finish()
    }

####

`pub fn del_cookie<'a>(&mut self, cookie: &Cookie<'a>) -> &mut Self`

[src]

Remove cookie

    use actix_web::{http, HttpRequest, HttpResponse, Result};

    fn index(req: &HttpRequest) -> HttpResponse {
        let mut builder = HttpResponse::Ok();

        if let Some(ref cookie) = req.cookie("name") {
            builder.del_cookie(cookie);
        }

        builder.finish()
    }

####

`pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self where F: FnOnce(&mut HttpResponseBuilder),`

[src]

This method calls provided closure with builder reference if value is true.

####

`pub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Self where F: FnOnce(T, &mut HttpResponseBuilder),`

[src]

This method calls provided closure with builder reference if value is Some.

####

`pub fn write_buffer_capacity(&mut self, cap: usize) -> &mut Self`

[src]

Set write buffer capacity

This parameter makes sense only for streaming response or actor. If write buffer reaches specified capacity, stream or actor get paused.

Default write buffer capacity is 64kb

####

`pub fn body<B: Into<Body>>(&mut self, body: B) -> HttpResponse`

[src]

Set a body and generate `HttpResponse`.

`HttpResponseBuilder` can not be used after this call.

####

`pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponse where S: Stream<Item = Bytes, Error = E> + 'static, E: Into<Error>,`

[src]

Set a streaming body and generate `HttpResponse`.

`HttpResponseBuilder` can not be used after this call.

####

`pub fn json<T: Serialize>(&mut self, value: T) -> HttpResponse`

[src]

Set a json body and generate `HttpResponse`

`HttpResponseBuilder` can not be used after this call.

####

`pub fn json2<T: Serialize>(&mut self, value: &T) -> HttpResponse`

[src]

Set a json body and generate `HttpResponse`

`HttpResponseBuilder` can not be used after this call.

####

`pub fn finish(&mut self) -> HttpResponse`

[src]

Set an empty body and generate `HttpResponse`

`HttpResponseBuilder` can not be used after this call.

####

`pub fn take(&mut self) -> HttpResponseBuilder`

[src]

This method construct new `HttpResponseBuilder`

## Trait Implementations

###

`impl Responder for HttpResponseBuilder`

[src]

#### `type Item = HttpResponse`

The associated item which can be returned.

#### `type Error = Error`

The associated error which can be returned.

####

`fn respond_to<S>(self, _: &HttpRequest<S>) -> Result<HttpResponse, Error>`

[src]

Convert itself to `AsyncResult` or `Error`.

###

`impl From<HttpResponseBuilder> for HttpResponse`

[src]

####

`fn from(builder: HttpResponseBuilder) -> Self`

[src]

Performs the conversion.

###

`impl<'a> From<&'a ClientResponse> for HttpResponseBuilder`

[src]

Create `HttpResponseBuilder` from `ClientResponse`

It is useful for proxy response. This implementation copies all responses's headers and status.

####

`fn from(resp: &'a ClientResponse) -> HttpResponseBuilder`

[src]

Performs the conversion.

###

`impl<'a, S> From<&'a HttpRequest<S>> for HttpResponseBuilder`

[src]

####

`fn from(req: &'a HttpRequest<S>) -> HttpResponseBuilder`

[src]

Performs the conversion.

## Auto Trait Implementations

###

`impl !Send for HttpResponseBuilder`

###

`impl !Sync for HttpResponseBuilder`

## Blanket Implementations

###

`impl<T> From for T`

[src]

####

`fn from(t: T) -> T`

[src]

Performs the conversion.

###

`impl<T, U> Into for T where U: From<T>,`

[src]

####

`fn into(self) -> U`

[src]

Performs the conversion.

###

`impl<T, U> TryFrom for T where T: From<U>,`

[src]

#### `type Error = !`

🔬 This is a nightly-only experimental API. (`try_from`)

The type returned in the event of a conversion error.

####

`fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`

[src]

🔬 This is a nightly-only experimental API. (`try_from`)

Performs the conversion.

###

`impl<T> Borrow for T where T: ?Sized,`

[src]

####

`fn borrow(&self) -> &T`

[src]

Immutably borrows from an owned value. Read more

###

`impl<T> BorrowMut for T where T: ?Sized,`

[src]

####

`fn borrow_mut(&mut self) -> &mut T`

[src]

Mutably borrows from an owned value. Read more

###

`impl<T, U> TryInto for T where U: TryFrom<T>,`

[src]

#### `type Error = <U as TryFrom<T>>::Error`

🔬 This is a nightly-only experimental API. (`try_from`)

The type returned in the event of a conversion error.

####

`fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`

[src]

🔬 This is a nightly-only experimental API. (`try_from`)

Performs the conversion.

###

`impl<T> Any for T where T: 'static + ?Sized,`

[src]

####

`fn get_type_id(&self) -> TypeId`

[src]

🔬 This is a nightly-only experimental API. (`get_type_id`)

this method will likely be replaced by an associated static

Gets the `TypeId` of `self`. Read more

###

`impl<T> Erased for T`

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)