pub struct ResponseBuilder(_);
Expand description

Response Builder

Provides an ergonomic way to chain the creation of a response. This is generally accessed through Response::builder

Example

let mut response = Response::builder(203)
    .body("body")
    .content_type(mime::HTML)
    .header("custom-header", "value")
    .build();

assert_eq!(response.take_body().into_string().await.unwrap(), "body");
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
assert_eq!(response["custom-header"], "value");
assert_eq!(response.content_type(), Some(mime::HTML));

Implementations§

source§

impl ResponseBuilder

source

pub fn build(self) -> Response

Returns the inner Response

source

pub fn header( self, key: impl Into<HeaderName>, value: impl ToHeaderValues ) -> ResponseBuilder

Sets a header on the response.

let response = Response::builder(200).header("header-name", "header-value").build();
assert_eq!(response["header-name"], "header-value");
source

pub fn content_type(self, content_type: impl Into<Mime>) -> ResponseBuilder

Sets the Content-Type header on the response.

let response = Response::builder(200).content_type(mime::HTML).build();
assert_eq!(response["content-type"], "text/html;charset=utf-8");
source

pub fn body(self, body: impl Into<Body>) -> ResponseBuilder

Sets the body of the response.

let mut response = Response::builder(200).body(json!({ "any": "Into<Body>"})).build();
assert_eq!(response.take_body().into_string().await.unwrap(), "{\"any\":\"Into<Body>\"}");

Trait Implementations§

source§

impl Debug for ResponseBuilder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Into<Response> for ResponseBuilder

source§

fn into(self) -> Response

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V