pub struct Request<S: State> { /* private fields */ }
Expand description
An incoming request
Implementations
sourceimpl<S: State> Request<S>
impl<S: State> Request<S>
sourcepub fn context_mut(&mut self) -> &mut S::Context
pub fn context_mut(&mut self) -> &mut S::Context
Get a mut reference to the request’s context
sourcepub fn query<T: DeserializeOwned>(&self) -> Result<T>
pub fn query<T: DeserializeOwned>(&self) -> Result<T>
Parse the URI query string into an instance of T
that derives Deserialize
.
(To get the raw query string access it via req.uri().query()
).
If there is no query string, deserialize an empty string.
sourcepub fn header<T: Header>(&self) -> Option<T>
pub fn header<T: Header>(&self) -> Option<T>
Get a typed header from the request
(See also headers
)
sourcepub fn headers(&self) -> &HeaderMap<HeaderValue>
pub fn headers(&self) -> &HeaderMap<HeaderValue>
Get all headers as a HeaderMap
Get the request’s cookies
sourcepub fn param(&self, param: &str) -> Result<&str>
pub fn param(&self, param: &str) -> Result<&str>
Get a route parameter (eg. :key
or *key
segments in the URI path)
If the parameter is not present, logs an error and returns a 400 Bad Request
to the client
sourcepub async fn reader(&mut self) -> Result<impl Read + '_>
pub async fn reader(&mut self) -> Result<impl Read + '_>
Get a reader to read the request body
(This does buffer the whole body into memory, but not necessarily contiguous memory).
If you need to protect against malicious clients you should access the body via body_mut
sourcepub async fn body_bytes(&mut self) -> Result<Vec<u8>>
pub async fn body_bytes(&mut self) -> Result<Vec<u8>>
Get the request body as raw bytes in a Vec<u8>
sourcepub async fn body_string(&mut self) -> Result<String>
pub async fn body_string(&mut self) -> Result<String>
Get the request body as UTF-8 data in String
sourcepub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
pub async fn body_json<T: DeserializeOwned>(&mut self) -> Result<T>
Get the request body as JSON and deserialize into T
.
If deserialization fails, log an error and return 400 Bad Request
.
(If this logic is not appropriate, consider using reader
and using serde_json
directly)
sourcepub fn remote_addr(&self) -> &SocketAddr
pub fn remote_addr(&self) -> &SocketAddr
Get the address of the remote peer.
This method uses the network level address only and hence may be incorrect if you are
behind a proxy. (This does not check for any Forwarded
headers etc…)
Trait Implementations
sourceimpl<S> HasSession for Request<S>where
S: State,
S::Context: HasSession,
impl<S> HasSession for Request<S>where
S: State,
S::Context: HasSession,
Implement HasSession on requests where the Context has sessions