Type Alias Request

pub type Request<T = Body> = Request<T>;
Expand description

Type alias for http::Request whose body type defaults to Body, the most common body type used with rama.

Aliased Type§

struct Request<T = Body> { /* private fields */ }

Implementations

§

impl Request<()>

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

pub fn get<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn put<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn post<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn delete<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn options<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn head<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn connect<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn patch<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

pub fn trace<T>(uri: T) -> Builder
where T: TryInto<Uri>, <T as TryInto<Uri>>::Error: Into<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

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

impl<T> Request<T>

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

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

pub fn from_parts(parts: Parts, body: T) -> Request<T>

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

pub fn method(&self) -> &Method

Returns a reference to the associated HTTP method.

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

pub fn method_mut(&mut self) -> &mut Method

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

pub fn uri(&self) -> &Uri

Returns a reference to the associated URI.

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

pub fn uri_mut(&mut self) -> &mut 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");

pub fn version(&self) -> Version

Returns the associated version.

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

pub fn version_mut(&mut self) -> &mut Version

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

pub fn headers(&self) -> &HeaderMap

Returns a reference to the associated header field map.

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

pub fn headers_mut(&mut self) -> &mut HeaderMap

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

pub fn extensions(&self) -> &Extensions

Returns a reference to the associated extensions.

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

pub fn extensions_mut(&mut self) -> &mut Extensions

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

pub fn body(&self) -> &T

Returns a reference to the associated HTTP body.

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

pub fn body_mut(&mut self) -> &mut T

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

pub fn into_body(self) -> T

Consumes the request, returning just the body.

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

pub fn into_parts(self) -> (Parts, T)

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

pub fn map<F, U>(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
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

§

impl<B> Body for Request<B>
where B: Body,

§

type Data = <B as Body>::Data

Values yielded by the Body.
§

type Error = <B as Body>::Error

The error type this Body might generate.
§

fn poll_frame( self: Pin<&mut Request<B>>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Frame<<Request<B> as Body>::Data>, <Request<B> as Body>::Error>>>

Attempt to pull out the next data buffer of this stream.
§

fn is_end_stream(&self) -> bool

Returns true when the end of stream has been reached. Read more
§

fn size_hint(&self) -> SizeHint

Returns the bounds on the remaining length of the stream. Read more
§

impl<Body> BodyExtractExt for Request<Body>
where Body: Body + Send + 'static, <Body as Body>::Data: Send + 'static, <Body as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

§

async fn try_into_json<T>(self) -> Result<T, OpaqueError>
where T: DeserializeOwned + Send + 'static,

Try to deserialize the (contained) body as a JSON object.
§

async fn try_into_string(self) -> Result<String, OpaqueError>

Try to turn the (contained) body in an utf-8 string.
§

impl<T> Clone for Request<T>
where T: Clone,

§

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

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> Debug for Request<T>
where T: Debug,

§

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

Formats the value using the given formatter. Read more
§

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

§

fn default() -> Request<T>

Returns the “default value” for a type. Read more
§

impl FromRequest for Request<Body>

§

type Rejection = Infallible

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
§

async fn from_request( req: Request<Body>, ) -> Result<Request<Body>, <Request<Body> as FromRequest>::Rejection>

Perform the extraction.
§

impl<Body> HeaderValueGetter for Request<Body>

§

fn header_str<K>(&self, key: K) -> Result<&str, HeaderValueErr>
where K: AsHeaderName + Copy,

Get a header value as a string.
§

fn header_bytes<K>(&self, key: K) -> Result<&[u8], HeaderValueErr>
where K: AsHeaderName + Copy,

Get a header value as a byte slice.
§

impl<State, Body> TryRefIntoTransportContext<State> for Request<Body>

§

type Error = OpaqueError

The error that can happen when trying to turn the self reference into the TransportContext.
§

fn try_ref_into_transport_ctx( &self, ctx: &Context<State>, ) -> Result<TransportContext, <Request<Body> as TryRefIntoTransportContext<State>>::Error>

Try to turn the reference to self within the given context into the TransportContext.