Struct perseus::HttpRequest

source ·
pub struct HttpRequest<T> { /* private fields */ }
Expand description

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 send(req: Request<()>) -> Response<()> {
    // ...
}

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();

    // ...
}

Deserialize a request of bytes via json:

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))
}

Or alternatively, serialize the body of a request to json

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))
}

Implementations

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
let request = Request::builder()
    .method("GET")
    .uri("https://www.rust-lang.org/")
    .header("X-Custom-Foo", "Bar")
    .body(())
    .unwrap();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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
let request = Request::new("hello world");

assert_eq!(*request.method(), Method::GET);
assert_eq!(*request.body(), "hello world");

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

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

let request = Request::from_parts(parts, body);

Returns a reference to the associated HTTP method.

Examples
let request: Request<()> = Request::default();
assert_eq!(*request.method(), Method::GET);

Returns a mutable reference to the associated HTTP method.

Examples
let mut request: Request<()> = Request::default();
*request.method_mut() = Method::PUT;
assert_eq!(*request.method(), Method::PUT);

Returns a reference to the associated URI.

Examples
let request: Request<()> = Request::default();
assert_eq!(*request.uri(), *"/");

Returns a mutable reference to the associated URI.

Examples
let mut request: Request<()> = Request::default();
*request.uri_mut() = "/hello".parse().unwrap();
assert_eq!(*request.uri(), *"/hello");

Returns the associated version.

Examples
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);

Returns a mutable reference to the associated version.

Examples
let mut request: Request<()> = Request::default();
*request.version_mut() = Version::HTTP_2;
assert_eq!(request.version(), Version::HTTP_2);

Returns a reference to the associated header field map.

Examples
let request: Request<()> = Request::default();
assert!(request.headers().is_empty());

Returns a mutable reference to the associated header field map.

Examples
let mut request: Request<()> = Request::default();
request.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!request.headers().is_empty());

Returns a reference to the associated extensions.

Examples
let request: Request<()> = Request::default();
assert!(request.extensions().get::<i32>().is_none());

Returns a mutable reference to the associated extensions.

Examples
let mut request: Request<()> = Request::default();
request.extensions_mut().insert("hello");
assert_eq!(request.extensions().get(), Some(&"hello"));

Returns a reference to the associated HTTP body.

Examples
let request: Request<String> = Request::default();
assert!(request.body().is_empty());

Returns a mutable reference to the associated HTTP body.

Examples
let mut request: Request<String> = Request::default();
request.body_mut().push_str("hello world");
assert!(!request.body().is_empty());

Consumes the request, returning just the body.

Examples
let request = Request::new(10);
let body = request.into_body();
assert_eq!(body, 10);

Consumes the request returning the head and body parts.

Examples
let request = Request::new(());
let (parts, body) = request.into_parts();
assert_eq!(parts.method, Method::GET);

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

Examples
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());

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

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
Converts self into T using Into<T>. Read more
Converts self into a target type. Read more
Causes self to use its Binary implementation when Debug-formatted.
Causes self to use its Display implementation when Debug-formatted. Read more
Causes self to use its LowerExp implementation when Debug-formatted. Read more
Causes self to use its LowerHex implementation when Debug-formatted. Read more
Causes self to use its Octal implementation when Debug-formatted.
Causes self to use its Pointer implementation when Debug-formatted. Read more
Causes self to use its UpperExp implementation when Debug-formatted. Read more
Causes self to use its UpperHex implementation when Debug-formatted. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function. Read more
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more
Pipes a value into a function that cannot ordinarily be called in suffix position. Read more
Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more
Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more
Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more
Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more
Pipes a dereference into a function that cannot normally be called in suffix position. Read more
Pipes a mutable dereference into a function that cannot normally be called in suffix position. Read more
Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more
Pipes a mutable reference into a function that cannot ordinarily be called in suffix position. Read more
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_ref() only in debug builds, and is erased in release builds. Read more
Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_deref() only in debug builds, and is erased in release builds. Read more
Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more
Provides immutable access for inspection. Read more
Calls tap in debug builds, and does nothing in release builds.
Provides mutable access for modification. Read more
Calls tap_mut in debug builds, and does nothing in release builds.
Provides immutable access to the reference for inspection.
Calls tap_ref in debug builds, and does nothing in release builds.
Provides mutable access to the reference for modification.
Calls tap_ref_mut in debug builds, and does nothing in release builds.
Provides immutable access to the borrow for inspection. Read more
Calls tap_borrow in debug builds, and does nothing in release builds.
Provides mutable access to the borrow for modification.
Calls tap_borrow_mut in debug builds, and does nothing in release builds. Read more
Immutably dereferences self for inspection.
Calls tap_deref in debug builds, and does nothing in release builds.
Mutably dereferences self for modification.
Calls tap_deref_mut in debug builds, and does nothing in release builds. Read more
Attempts to convert self into T using TryInto<T>. Read more
Attempts to convert self into a target type. Read more
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.