Struct Request

struct Request<T> { ... }

Represents an HTTP request.

An HTTP request consists of a head and a potentially optional body. The body component is generic, enabling arbitrary types to represent the HTTP body. For example, the body could be Vec<u8>, a Stream of byte chunks, or a value that has been deserialized.

Examples

Creating a Request to send

use http::{Request, Response};

let mut request = Request::builder()
    .uri("https://www.rust-lang.org/")
    .header("User-Agent", "my-awesome-agent/1.0");

if needs_awesome_header() {
    request = request.header("Awesome", "yes");
}

let response = send(request.body(()).unwrap());

# fn needs_awesome_header() -> bool {
#     true
# }
#
fn send(req: Request<()>) -> Response<()> {
    // ...
# panic!()
}

Inspecting a request to see what was sent.

use http::{Request, Response, StatusCode};

fn respond_to(req: Request<()>) -> http::Result<Response<()>> {
    if req.uri() != "/awesome-url" {
        return Response::builder()
            .status(StatusCode::NOT_FOUND)
            .body(())
    }

    let has_awesome_header = req.headers().contains_key("Awesome");
    let body = req.body();

    // ...
# panic!()
}

Deserialize a request of bytes via json:

# extern crate serde;
# extern crate serde_json;
# extern crate http;
use http::Request;
use serde::de;

fn deserialize<T>(req: Request<Vec<u8>>) -> serde_json::Result<Request<T>>
    where for<'de> T: de::Deserialize<'de>,
{
    let (parts, body) = req.into_parts();
    let body = serde_json::from_slice(&body)?;
    Ok(Request::from_parts(parts, body))
}
#
# fn main() {}

Or alternatively, serialize the body of a request to json

# extern crate serde;
# extern crate serde_json;
# extern crate http;
use http::Request;
use serde::ser;

fn serialize<T>(req: Request<T>) -> serde_json::Result<Request<Vec<u8>>>
    where T: ser::Serialize,
{
    let (parts, body) = req.into_parts();
    let body = serde_json::to_vec(&body)?;
    Ok(Request::from_parts(parts, body))
}
#
# fn main() {}

Implementations

impl Request<()>

fn builder() -> Builder

Creates a new builder-style object to manufacture a Request

This method returns an instance of Builder which can be used to create a Request.

Examples

# use http::*;
let request = Request::builder()
    .method("GET")
    .uri("https://www.rust-lang.org/")
    .header("X-Custom-Foo", "Bar")
    .body(())
    .unwrap();
fn get<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a GET method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::get("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn put<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a PUT method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::put("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn post<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a POST method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::post("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn delete<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a DELETE method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::delete("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn options<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with an OPTIONS method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::options("https://www.rust-lang.org/")
    .body(())
    .unwrap();
# assert_eq!(*request.method(), Method::OPTIONS);
fn head<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a HEAD method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::head("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn connect<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a CONNECT method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::connect("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn patch<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a PATCH method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::patch("https://www.rust-lang.org/")
    .body(())
    .unwrap();
fn trace<T>(uri: T) -> Builder
where
    T: TryInto<Uri>,
    <T as TryInto<Uri>>::Error: Into<crate::Error>

Creates a new Builder initialized with a TRACE method and the given URI.

This method returns an instance of Builder which can be used to create a Request.

Example

# use http::*;

let request = Request::trace("https://www.rust-lang.org/")
    .body(())
    .unwrap();

impl<T> Request<T>

fn new(body: T) -> Request<T>

Creates a new blank Request with the body

The component parts of this request will be set to their default, e.g. the GET method, no headers, etc.

Examples

# use http::*;
let request = Request::new("hello world");

assert_eq!(*request.method(), Method::GET);
assert_eq!(*request.body(), "hello world");
fn from_parts(parts: Parts, body: T) -> Request<T>

Creates a new Request with the given components parts and body.

Examples

# use http::*;
let request = Request::new("hello world");
let (mut parts, body) = request.into_parts();
parts.method = Method::POST;

let request = Request::from_parts(parts, body);
fn method(self: &Self) -> &Method

Returns a reference to the associated HTTP method.

Examples

# use http::*;
let request: Request<()> = Request::default();
assert_eq!(*request.method(), Method::GET);
fn method_mut(self: &mut Self) -> &mut Method

Returns a mutable reference to the associated HTTP method.

Examples

# use http::*;
let mut request: Request<()> = Request::default();
*request.method_mut() = Method::PUT;
assert_eq!(*request.method(), Method::PUT);
fn uri(self: &Self) -> &Uri

Returns a reference to the associated URI.

Examples

# use http::*;
let request: Request<()> = Request::default();
assert_eq!(*request.uri(), *"/");
fn uri_mut(self: &mut Self) -> &mut Uri

Returns a mutable reference to the associated URI.

Examples

# use http::*;
let mut request: Request<()> = Request::default();
*request.uri_mut() = "/hello".parse().unwrap();
assert_eq!(*request.uri(), *"/hello");
fn version(self: &Self) -> Version

Returns the associated version.

Examples

# use http::*;
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);
fn version_mut(self: &mut Self) -> &mut Version

Returns a mutable reference to the associated version.

Examples

# use http::*;
let mut request: Request<()> = Request::default();
*request.version_mut() = Version::HTTP_2;
assert_eq!(request.version(), Version::HTTP_2);
fn headers(self: &Self) -> &HeaderMap<HeaderValue>

Returns a reference to the associated header field map.

Examples

# use http::*;
let request: Request<()> = Request::default();
assert!(request.headers().is_empty());
fn headers_mut(self: &mut Self) -> &mut HeaderMap<HeaderValue>

Returns a mutable reference to the associated header field map.

Examples

# use http::*;
# use http::header::*;
let mut request: Request<()> = Request::default();
request.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!request.headers().is_empty());
fn extensions(self: &Self) -> &Extensions

Returns a reference to the associated extensions.

Examples

# use http::*;
let request: Request<()> = Request::default();
assert!(request.extensions().get::<i32>().is_none());
fn extensions_mut(self: &mut Self) -> &mut Extensions

Returns a mutable reference to the associated extensions.

Examples

# use http::*;
# use http::header::*;
let mut request: Request<()> = Request::default();
request.extensions_mut().insert("hello");
assert_eq!(request.extensions().get(), Some(&"hello"));
fn body(self: &Self) -> &T

Returns a reference to the associated HTTP body.

Examples

# use http::*;
let request: Request<String> = Request::default();
assert!(request.body().is_empty());
fn body_mut(self: &mut Self) -> &mut T

Returns a mutable reference to the associated HTTP body.

Examples

# use http::*;
let mut request: Request<String> = Request::default();
request.body_mut().push_str("hello world");
assert!(!request.body().is_empty());
fn into_body(self: Self) -> T

Consumes the request, returning just the body.

Examples

# use http::Request;
let request = Request::new(10);
let body = request.into_body();
assert_eq!(body, 10);
fn into_parts(self: Self) -> (Parts, T)

Consumes the request returning the head and body parts.

Examples

# use http::*;
let request = Request::new(());
let (parts, body) = request.into_parts();
assert_eq!(parts.method, Method::GET);
fn map<F, U>(self: Self, f: F) -> Request<U>
where
    F: FnOnce(T) -> U

Consumes the request returning a new request with body mapped to the return type of the passed in function.

Examples

# use http::*;
let request = Request::builder().body("some string").unwrap();
let mapped_request: Request<&[u8]> = request.map(|b| {
  assert_eq!(b, "some string");
  b.as_bytes()
});
assert_eq!(mapped_request.body(), &"some string".as_bytes());

impl<T> Any for Request<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Request<T>

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

impl<T> BorrowMut for Request<T>

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

impl<T> CloneToUninit for Request<T>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> Freeze for Request<T>

impl<T> From for Request<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> RefUnwindSafe for Request<T>

impl<T> Send for Request<T>

impl<T> Sync for Request<T>

impl<T> ToOwned for Request<T>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> Unpin for Request<T>

impl<T> UnwindSafe for Request<T>

impl<T, U> Into for Request<T>

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 Request<T>

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

impl<T, U> TryInto for Request<T>

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

impl<T: $crate::clone::Clone> Clone for Request<T>

fn clone(self: &Self) -> Request<T>

impl<T: Default> Default for Request<T>

fn default() -> Request<T>

impl<T: fmt::Debug> Debug for Request<T>

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