Struct Builder

struct Builder { ... }

An HTTP request builder

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

Implementations

impl Builder

fn new() -> Builder

Creates a new default instance of Builder to construct a Request.

Examples

# use http::*;

let req = request::Builder::new()
    .method("POST")
    .body(())
    .unwrap();
fn method<T>(self: Self, method: T) -> Builder
where
    T: TryInto<Method>,
    <T as TryInto<Method>>::Error: Into<crate::Error>

Set the HTTP method for this request.

By default this is GET.

Examples

# use http::*;

let req = Request::builder()
    .method("POST")
    .body(())
    .unwrap();
fn method_ref(self: &Self) -> Option<&Method>

Get the HTTP Method for this request.

By default this is GET. If builder has error, returns None.

Examples

# use http::*;

let mut req = Request::builder();
assert_eq!(req.method_ref(),Some(&Method::GET));

req = req.method("POST");
assert_eq!(req.method_ref(),Some(&Method::POST));
fn uri<T>(self: Self, uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Set the URI for this request.

By default this is /.

Examples

# use http::*;

let req = Request::builder()
    .uri("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn uri_ref(self: &Self) -> Option<&Uri>

Get the URI for this request

By default this is /.

Examples

# use http::*;

let mut req = Request::builder();
assert_eq!(req.uri_ref().unwrap(), "/" );

req = req.uri("https://www.rust-lang.org/");
assert_eq!(req.uri_ref().unwrap(), "https://www.rust-lang.org/" );
fn version(self: Self, version: Version) -> Builder

Set the HTTP version for this request.

By default this is HTTP/1.1

Examples

# use http::*;

let req = Request::builder()
    .version(Version::HTTP_2)
    .body(())
    .unwrap();
fn version_ref(self: &Self) -> Option<&Version>

Get the HTTP version for this request

By default this is HTTP/1.1.

Examples

# use http::*;

let mut req = Request::builder();
assert_eq!(req.version_ref().unwrap(), &Version::HTTP_11 );

req = req.version(Version::HTTP_2);
assert_eq!(req.version_ref().unwrap(), &Version::HTTP_2 );
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 request 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 req = Request::builder()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar")
    .body(())
    .unwrap();
fn headers_ref(self: &Self) -> Option<&HeaderMap<HeaderValue>>

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

Example

# use http::Request;
let req = Request::builder()
    .header("Accept", "text/html")
    .header("X-Custom-Foo", "bar");
let headers = req.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 headers on this request builder.

When builder has error returns None.

Example

# use http::{header::HeaderValue, Request};
let mut req = Request::builder();
{
  let headers = req.headers_mut().unwrap();
  headers.insert("Accept", HeaderValue::from_static("text/html"));
  headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
}
let headers = req.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 req = Request::builder()
    .extension("My Extension")
    .body(())
    .unwrap();

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

Get a reference to the extensions for this request builder.

If the builder has an error, this returns None.

Example

# use http::Request;
let req = Request::builder().extension("My Extension").extension(5u32);
let extensions = req.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 request builder.

If the builder has an error, this returns None.

Example

# use http::Request;
let mut req = Request::builder().extension("My Extension");
let mut extensions = req.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<Request<T>>

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

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 request = Request::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>