Struct Builder

struct Builder { ... }

An HTTP response builder

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

Implementations

impl Builder

fn new() -> Builder

Creates a new default instance of Builder to construct either a Head or a Response.

Examples

# use http::*;

let response = response::Builder::new()
    .status(200)
    .body(())
    .unwrap();
fn status<T>(self: Self, status: T) -> Builder
where
    T: TryInto<StatusCode>,
    <T as TryInto<StatusCode>>::Error: Into<crate::Error>

Set the HTTP status for this response.

By default this is 200.

Examples

# use http::*;

let response = Response::builder()
    .status(200)
    .body(())
    .unwrap();
fn version(self: Self, version: Version) -> Builder

Set the HTTP version for this response.

By default this is HTTP/1.1

Examples

# use http::*;

let response = Response::builder()
    .version(Version::HTTP_2)
    .body(())
    .unwrap();
fn header<K, V>(self: Self, key: K, value: V) -> Builder
where
    K: TryInto<HeaderName>,
    <K as TryInto<HeaderName>>::Error: Into<crate::Error>,
    V: TryInto<HeaderValue>,
    <V as TryInto<HeaderValue>>::Error: Into<crate::Error>

Appends a header to this response builder.

This function will append the provided key/value as a header to the internal HeaderMap being constructed. Essentially this is equivalent to calling HeaderMap::append.

Examples

# use http::*;
# use http::header::HeaderValue;

let response = Response::builder()
    .header("Content-Type", "text/html")
    .header("X-Custom-Foo", "bar")
    .header("content-length", 0)
    .body(())
    .unwrap();
fn headers_ref(self: &Self) -> Option<&HeaderMap<HeaderValue>>

Get header on this response builder.

When builder has error returns None.

Example

# use http::Response;
# use http::header::HeaderValue;
let res = Response::builder()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar");
let headers = res.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
fn headers_mut(self: &mut Self) -> Option<&mut HeaderMap<HeaderValue>>

Get header on this response builder. when builder has error returns None

Example

# use http::*;
# use http::header::HeaderValue;
# use http::response::Builder;
let mut res = Response::builder();
{
  let headers = res.headers_mut().unwrap();
  headers.insert("Accept", HeaderValue::from_static("text/html"));
  headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
}
let headers = res.headers_ref().unwrap();
assert_eq!( headers["Accept"], "text/html" );
assert_eq!( headers["X-Custom-Foo"], "bar" );
fn extension<T>(self: Self, extension: T) -> Builder
where
    T: Clone + Any + Send + Sync + 'static

Adds an extension to this builder

Examples

# use http::*;

let response = Response::builder()
    .extension("My Extension")
    .body(())
    .unwrap();

assert_eq!(response.extensions().get::<&'static str>(),
           Some(&"My Extension"));
fn extensions_ref(self: &Self) -> Option<&Extensions>

Get a reference to the extensions for this response builder.

If the builder has an error, this returns None.

Example

# use http::Response;
let res = Response::builder().extension("My Extension").extension(5u32);
let extensions = res.extensions_ref().unwrap();
assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
assert_eq!(extensions.get::<u32>(), Some(&5u32));
fn extensions_mut(self: &mut Self) -> Option<&mut Extensions>

Get a mutable reference to the extensions for this response builder.

If the builder has an error, this returns None.

Example

# use http::Response;
let mut res = Response::builder().extension("My Extension");
let mut extensions = res.extensions_mut().unwrap();
assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
extensions.insert(5u32);
assert_eq!(extensions.get::<u32>(), Some(&5u32));
fn body<T>(self: Self, body: T) -> Result<Response<T>>

"Consumes" this builder, using the provided body to return a constructed Response.

Errors

This function may return an error if any previously configured argument failed to parse or get converted to the internal representation. For example if an invalid head was specified via header("Foo", "Bar\r\n") the error will be returned when this function is called rather than when header was called.

Examples

# use http::*;

let response = Response::builder()
    .body(())
    .unwrap();

impl Debug for Builder

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

impl Default for Builder

fn default() -> Builder

impl Freeze for Builder

impl RefUnwindSafe for Builder

impl Send for Builder

impl Sync for Builder

impl Unpin for Builder

impl UnwindSafe for Builder

impl<T> Any for Builder

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Builder

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Builder

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

impl<T> From for Builder

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Builder

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Builder

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

impl<T, U> TryInto for Builder

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