pub struct Request<T = Body> { /* 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 rama_http_types::{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 rama_http_types::{Request, Response, Result, StatusCode};
fn respond_to(req: Request<()>) -> Result<Response<()>> {
if req.uri().as_str() != "/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 rama_http_types::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 rama_http_types::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§
Source§impl Request<()>
impl Request<()>
Sourcepub fn builder() -> Builder
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();Sourcepub fn builder_with_extensions(ext: Extensions) -> Builder
pub fn builder_with_extensions(ext: Extensions) -> Builder
Same as Request::builder but with the given Extensions to start from.
Sourcepub fn get<T>(uri: T) -> Builder
pub fn get<T>(uri: T) -> Builder
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();Sourcepub fn put<T>(uri: T) -> Builder
pub fn put<T>(uri: T) -> Builder
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();Sourcepub fn post<T>(uri: T) -> Builder
pub fn post<T>(uri: T) -> Builder
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();Sourcepub fn delete<T>(uri: T) -> Builder
pub fn delete<T>(uri: T) -> Builder
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();Sourcepub fn options<T>(uri: T) -> Builder
pub fn options<T>(uri: T) -> Builder
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();Sourcepub fn head<T>(uri: T) -> Builder
pub fn head<T>(uri: T) -> Builder
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();Sourcepub fn connect<T>(uri: T) -> Builder
pub fn connect<T>(uri: T) -> Builder
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();Sourcepub fn patch<T>(uri: T) -> Builder
pub fn patch<T>(uri: T) -> Builder
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();Sourcepub fn trace<T>(uri: T) -> Builder
pub fn trace<T>(uri: T) -> Builder
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();Sourcepub fn query<T>(uri: T) -> Builder
pub fn query<T>(uri: T) -> Builder
Creates a new Builder initialized with a QUERY method and the given URI.
QUERY (RFC 10008) is a safe,
idempotent method whose request content defines the query; remember to
set a Content-Type for the body.
This method returns an instance of Builder which can be used to
create a Request.
§Example
let request = Request::query("https://www.rust-lang.org/")
.body(())
.unwrap();Source§impl<T> Request<T>
impl<T> Request<T>
Sourcepub fn new(body: T) -> Self
pub fn new(body: T) -> Self
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");Sourcepub fn from_parts(parts: Parts, body: T) -> Self
pub fn from_parts(parts: Parts, body: T) -> Self
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);Sourcepub fn method(&self) -> &Method
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);Sourcepub fn method_mut(&mut self) -> &mut Method
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);Sourcepub fn uri(&self) -> &Uri
pub fn uri(&self) -> &Uri
Returns a reference to the associated URI.
§Examples
let request: Request<()> = Request::default();
assert_eq!(request.uri().as_str(), "/");Sourcepub fn request_uri(&self) -> Uri
pub fn request_uri(&self) -> Uri
Get the URI as complete as possible for this request.
Where uri returns the URI exactly as received (often
origin-form /path for HTTP/1.1), this fills in the scheme and
authority from the request’s effective protocol and authority (URI →
TLS SNI → Forwarded → Host header) — a derivation
callers can treat as a technical detail.
Sourcepub fn uri_mut(&mut self) -> &mut 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().as_str(), "/hello");Sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns the associated version.
§Examples
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);Sourcepub fn version_mut(&mut self) -> &mut Version
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);Sourcepub fn headers(&self) -> &HeaderMap<HeaderValue>
pub fn headers(&self) -> &HeaderMap<HeaderValue>
Returns a reference to the associated header field map.
§Examples
let request: Request<()> = Request::default();
assert!(request.headers().is_empty());Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
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());Sourcepub fn clone_parts(&self) -> Parts
pub fn clone_parts(&self) -> Parts
Returned a cloned of the associated HTTP header.
Sourcepub fn body(&self) -> &T
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());Sourcepub fn body_mut(&mut self) -> &mut T
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());Sourcepub fn into_body(self) -> T
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);Sourcepub fn into_parts(self) -> (Parts, T)
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);Sourcepub fn map<F, U>(self, f: F) -> Request<U>where
F: FnOnce(T) -> U,
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());pub fn with_extensions(self, extensions: Extensions) -> Self
pub fn set_extensions(&mut self, extensions: Extensions) -> &mut Self
Trait Implementations§
Source§impl<Body> AuthorityInputExt for Request<Body>
impl<Body> AuthorityInputExt for Request<Body>
Source§impl<B: StreamingBody> Body for Request<B>
impl<B: StreamingBody> Body for Request<B>
Source§impl<Body> BodyExtractExt for Request<Body>
impl<Body> BodyExtractExt for Request<Body>
Source§async fn try_into_json<T: DeserializeOwned + Send + 'static>(
self,
) -> Result<T, BoxError>
async fn try_into_json<T: DeserializeOwned + Send + 'static>( self, ) -> Result<T, BoxError>
Source§async fn try_into_json_streaming<T: DeserializeOwned + Send + 'static>(
self,
) -> Result<T, BoxError>
async fn try_into_json_streaming<T: DeserializeOwned + Send + 'static>( self, ) -> Result<T, BoxError>
Source§async fn try_into_string(self) -> Result<String, BoxError>
async fn try_into_string(self) -> Result<String, BoxError>
Source§async fn try_capture_json(
self,
selectors: impl IntoIterator<Item = JsonPath> + Send,
max_capture_bytes: usize,
) -> Result<Vec<OwnedCapturedValue>, BoxError>
async fn try_capture_json( self, selectors: impl IntoIterator<Item = JsonPath> + Send, max_capture_bytes: usize, ) -> Result<Vec<OwnedCapturedValue>, BoxError>
selectors while streaming over the body. Read moreSource§async fn try_into_json_with<T: DeserializeOwned + Send + 'static>(
self,
opts: CollectOptions,
) -> Result<T, CollectError>
async fn try_into_json_with<T: DeserializeOwned + Send + 'static>( self, opts: CollectOptions, ) -> Result<T, CollectError>
Source§async fn try_into_string_with(
self,
opts: CollectOptions,
) -> Result<String, CollectError>
async fn try_into_string_with( self, opts: CollectOptions, ) -> Result<String, CollectError>
Source§impl<B> ExtensionsRef for Request<B>
impl<B> ExtensionsRef for Request<B>
Source§fn extensions(&self) -> &Extensions
fn extensions(&self) -> &Extensions
Extensions storeSource§impl<Body> HttpRequestParts for Request<Body>
impl<Body> HttpRequestParts for Request<Body>
Source§impl<Body> HttpRequestPartsMut for Request<Body>
impl<Body> HttpRequestPartsMut for Request<Body>
fn method_mut(&mut self) -> &mut Method
fn uri_mut(&mut self) -> &mut Uri
fn version_mut(&mut self) -> &mut Version
fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
Source§impl<Body> HttpVersionInputExt for Request<Body>
impl<Body> HttpVersionInputExt for Request<Body>
Source§fn http_version(&self) -> Option<Version>
fn http_version(&self) -> Option<Version>
None for non-HTTP inputs.Source§impl<Body> Matcher<Request<Body>> for SocketAddressMatcher
impl<Body> Matcher<Request<Body>> for SocketAddressMatcher
Source§fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body> Matcher<Request<Body>> for IpNetMatcher
impl<Body> Matcher<Request<Body>> for IpNetMatcher
Source§fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body> Matcher<Request<Body>> for LoopbackMatcher
impl<Body> Matcher<Request<Body>> for LoopbackMatcher
Source§fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body> Matcher<Request<Body>> for PortMatcher
impl<Body> Matcher<Request<Body>> for PortMatcher
Source§fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body> Matcher<Request<Body>> for PrivateIpNetMatcher
impl<Body> Matcher<Request<Body>> for PrivateIpNetMatcher
Source§fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body: 'static> Matcher<Request<Body>> for SocketMatcher<Request<Body>>
The composite SocketMatcher delegates to the leaf impls above (and any
custom/nested matchers) through its public matches_input helper, so its
All/Any/Custom/negation logic applies to an HTTP Request too.
impl<Body: 'static> Matcher<Request<Body>> for SocketMatcher<Request<Body>>
The composite SocketMatcher delegates to the leaf impls above (and any
custom/nested matchers) through its public matches_input helper, so its
All/Any/Custom/negation logic applies to an HTTP Request too.
Source§fn matches(&self, ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body> Matcher<Request<Body>> for HttpProxyConnectMatcher
impl<Body> Matcher<Request<Body>> for HttpProxyConnectMatcher
Source§fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
fn matches(&self, _ext: Option<&Extensions>, req: &Request<Body>) -> bool
Source§fn or<M>(self, other: M) -> impl Matcher<Input>
fn or<M>(self, other: M) -> impl Matcher<Input>
Source§impl<Body> PathInputExt for Request<Body>
impl<Body> PathInputExt for Request<Body>
Source§impl<Body> ProtocolInputExt for Request<Body>
impl<Body> ProtocolInputExt for Request<Body>
Source§impl<Body> TransportProtocolInputExt for Request<Body>
impl<Body> TransportProtocolInputExt for Request<Body>
Source§fn transport_protocol(&self) -> Option<TransportProtocol>
fn transport_protocol(&self) -> Option<TransportProtocol>
Auto Trait Implementations§
impl<T> Freeze for Request<T>where
T: Freeze,
impl<T> RefUnwindSafe for Request<T>where
T: RefUnwindSafe,
impl<T> Send for Request<T>where
T: Send,
impl<T> Sync for Request<T>where
T: Sync,
impl<T> Unpin for Request<T>where
T: Unpin,
impl<T> UnsafeUnpin for Request<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Request<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BodyExt for T
impl<T> BodyExt for T
Source§fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
fn frame(&mut self) -> Frame<'_, Self> ⓘwhere
Self: Unpin,
Frame, if any.Source§fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
Source§fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
fn inspect_frame<F>(self, f: F) -> InspectFrame<Self, F>
Source§fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
Source§fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
Source§fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
Source§fn collect_with(self, opts: CollectOptions) -> CollectWith<Self> ⓘ
fn collect_with(self, opts: CollectOptions) -> CollectWith<Self> ⓘ
CollectOptions. Read moreSource§fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
Source§fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
fn into_data_stream(self) -> BodyDataStream<Self>where
Self: Sized,
BodyDataStream.Source§fn into_stream(self) -> BodyStream<Self>where
Self: Sized,
fn into_stream(self) -> BodyStream<Self>where
Self: Sized,
BodyStream.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ConnectorTargetInputExt for T
impl<T> ConnectorTargetInputExt for T
Source§fn connector_target(&self) -> Option<HostWithPort>
fn connector_target(&self) -> Option<HostWithPort>
host:port to connect to: the authority’s port if set, else the
protocol’s default port. None when no host (or no port) resolves. Read moreSource§fn connector_target_with_default_port(
&self,
default_port: u16,
) -> Option<HostWithPort>
fn connector_target_with_default_port( &self, default_port: u16, ) -> Option<HostWithPort>
connector_target but with default_port as
the ultimate fallback (ConnectorTarget → authority port → protocol default → default_port),
so it yields Some whenever an authority resolves at all. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more