Struct actix_http::ResponseBuilder[][src]

pub struct ResponseBuilder { /* fields omitted */ }
Expand description

An HTTP response builder.

Used to construct an instance of Response using a builder pattern. Response builders are often created using Response::build.

Examples

use actix_http::{Response, ResponseBuilder, StatusCode, body, header};

let mut res: Response<_> = Response::build(StatusCode::OK)
    .content_type(mime::APPLICATION_JSON)
    .insert_header((header::SERVER, "my-app/1.0"))
    .append_header((header::SET_COOKIE, "a=1"))
    .append_header((header::SET_COOKIE, "b=2"))
    .body("1234");

assert_eq!(res.status(), StatusCode::OK);

assert!(res.headers().contains_key("server"));
assert_eq!(res.headers().get_all("set-cookie").count(), 2);

assert_eq!(body::to_bytes(res.into_body()).await.unwrap(), &b"1234"[..]);

Implementations

Create response builder

Examples
use actix_http::{Response, ResponseBuilder, StatusCode};
let res: Response<_> = ResponseBuilder::default().finish();
assert_eq!(res.status(), StatusCode::OK);

Set HTTP status code of this response.

Examples
use actix_http::{ResponseBuilder, StatusCode};
let res = ResponseBuilder::default().status(StatusCode::NOT_FOUND).finish();
assert_eq!(res.status(), StatusCode::NOT_FOUND);

Insert a header, replacing any that were set with an equivalent field name.

Examples
use actix_http::{ResponseBuilder, header};

let res = ResponseBuilder::default()
    .insert_header((header::CONTENT_TYPE, mime::APPLICATION_JSON))
    .insert_header(("X-TEST", "value"))
    .finish();

assert!(res.headers().contains_key("content-type"));
assert!(res.headers().contains_key("x-test"));

Append a header, keeping any that were set with an equivalent field name.

Examples
use actix_http::{ResponseBuilder, header};

let res = ResponseBuilder::default()
    .append_header((header::CONTENT_TYPE, mime::APPLICATION_JSON))
    .append_header(("X-TEST", "value1"))
    .append_header(("X-TEST", "value2"))
    .finish();

assert_eq!(res.headers().get_all("content-type").count(), 1);
assert_eq!(res.headers().get_all("x-test").count(), 2);

Set the custom reason for the response.

Set connection type to KeepAlive

Set connection type to Upgrade

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

Disable chunked transfer encoding for HTTP/1.1 streaming responses.

Set response content type.

Responses extensions

Mutable reference to a the response’s extensions

Generate response with a wrapped body.

This ResponseBuilder will be left in a useless state.

Generate response with a body.

This ResponseBuilder will be left in a useless state.

Generate response with an empty body.

This ResponseBuilder will be left in a useless state.

Create an owned ResponseBuilder, leaving the original in a useless state.

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Convert ResponseHead to a ResponseBuilder

Performs the conversion.

Convert Response to a ResponseBuilder. Body get dropped.

Performs the conversion.

Performs the conversion.

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

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

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.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more