pub struct HttpResponseBuilder { /* private fields */ }
Expand description

An HTTP response builder

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

Implementations

Set HTTP status code of this response.

Set HTTP version of this response.

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

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() {}

Set the custom reason for the response.

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.

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

Enables automatic chunked transfer encoding

Force disable chunked encoding

Set response content type

Set content length

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()
}

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()
}

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

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

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

Set a body and generate HttpResponse.

HttpResponseBuilder can not be used after this call.

Set a streaming body and generate HttpResponse.

HttpResponseBuilder can not be used after this call.

Set a json body and generate HttpResponse

HttpResponseBuilder can not be used after this call.

Set a json body and generate HttpResponse

HttpResponseBuilder can not be used after this call.

Set an empty body and generate HttpResponse

HttpResponseBuilder can not be used after this call.

This method construct new HttpResponseBuilder

Trait Implementations

Create HttpResponseBuilder from ClientResponse

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

Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
The associated item which can be returned.
The associated error which can be returned.
Convert itself to AsyncResult or Error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.