pub struct Request { /* private fields */ }Expand description
An incoming request, already parsed and matched against a route.
Implementations§
Source§impl Request
impl Request
Sourcepub fn new(method: Method, target: impl Into<String>) -> Self
pub fn new(method: Method, target: impl Into<String>) -> Self
Build a request directly. This is what the test client and the server parser both go through.
pub fn method(&self) -> Method
Sourcepub fn route(&self) -> Option<&str>
pub fn route(&self) -> Option<&str>
The pattern this request matched: /users/{id}. Useful for metrics
that must not explode into one series per id.
pub fn headers(&self) -> &Headers
pub fn headers_mut(&mut self) -> &mut Headers
pub fn header(&self, name: &str) -> Option<&str>
pub fn body(&self) -> &[u8] ⓘ
pub fn body_string(&self) -> String
pub fn context(&self) -> &Context
pub fn config(&self) -> &Config
Sourcepub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>
A service registered on the application: req.state::<Database>().
pub fn peer_addr(&self) -> Option<SocketAddr>
Sourcepub fn ip(&self) -> Option<String>
pub fn ip(&self) -> Option<String>
The client IP, honouring X-Forwarded-For when behind a proxy.
The client’s address.
The address that opened the socket, unless
TrustProxies ran and the
connection came from a proxy on its list — then it is the client
address that proxy reported.
It deliberately does not read X-Forwarded-For on its own. A header
is something any client can send, so believing one unconditionally
does not reveal the client’s address, it lets the client choose one —
and everything keyed on this, the rate limiter included, would be
defeated by a header.
Sourcepub fn scheme(&self) -> &str
pub fn scheme(&self) -> &str
https when a trusted proxy said the client used TLS, or the
connection itself did; http otherwise.
A proxy that terminates TLS forwards a plain request, so without this
an application behind one would build http:// links for a site that
is entirely https://.
pub fn is_secure(&self) -> bool
Sourcepub fn forwarded_host(&self) -> Option<&str>
pub fn forwarded_host(&self) -> Option<&str>
The Host a trusted proxy said the client asked for.
Sourcepub fn forwarded_port(&self) -> Option<u16>
pub fn forwarded_port(&self) -> Option<u16>
The port a trusted proxy said the client connected to.
Sourcepub fn param(&self, name: &str) -> Option<&str>
pub fn param(&self, name: &str) -> Option<&str>
A route parameter: for /users/{id} matching /users/7, param("id")
is "7".
Sourcepub fn param_as<T: FromStr>(&self, name: &str) -> Option<T>
pub fn param_as<T: FromStr>(&self, name: &str) -> Option<T>
A route parameter parsed into a type, so a handler can ask for an id as a number without unwrapping twice.
pub fn params(&self) -> &BTreeMap<String, String>
pub fn query(&self, name: &str) -> Option<&str>
Sourcepub fn query_all(&self, name: &str) -> Vec<&str>
pub fn query_all(&self, name: &str) -> Vec<&str>
Every value for a repeated query key: ?tag=a&tag=b.
pub fn query_pairs(&self) -> &[(String, String)]
pub fn content_type(&self) -> Option<&str>
pub fn is_json(&self) -> bool
Sourcepub fn wants_json(&self) -> bool
pub fn wants_json(&self) -> bool
Whether the client wants JSON back — an API client or a fetch() call.
Sourcepub fn json(&mut self) -> Option<&Json>
pub fn json(&mut self) -> Option<&Json>
The body parsed as JSON, or None if it is absent or malformed.
Sourcepub fn input(&mut self, name: &str) -> Option<String>
pub fn input(&mut self, name: &str) -> Option<String>
One input value, looked up in the JSON body, then the form body, then
the query string — the resolution order of Laravel’s $request->input().
Sourcepub fn inputs(&mut self, name: &str) -> Vec<String>
pub fn inputs(&mut self, name: &str) -> Vec<String>
All decoded form fields of a application/x-www-form-urlencoded body.
Every value submitted under one name.
A form with several checkboxes sharing a name — roles[], which is how
PHP and every HTML tutorial spell it — sends the name once per ticked
box. Request::input returns only the first, which for a checkbox
group silently means “whichever happened to come first”.
A trailing [] is optional here: inputs("roles") and
inputs("roles[]") both find them, because which one a form used is a
detail of the markup rather than a decision the handler should have to
track.
pub fn form(&mut self) -> &[(String, String)]
Sourcepub fn extend<T: Send + Sync + 'static>(&mut self, value: T)
pub fn extend<T: Send + Sync + 'static>(&mut self, value: T)
Attach a value for later middleware or the handler to read.
Sourcepub fn api_version(&self) -> Option<&str>
pub fn api_version(&self) -> Option<&str>
The API version this request is for — from the route’s
Router::version group, or from the
VersionHeader middleware.
Sourcepub fn request_id(&self) -> Option<&str>
pub fn request_id(&self) -> Option<&str>
The identifier the RequestId
middleware assigned, for log lines and error reports.
Sourcepub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T>
Read a value attached by earlier middleware.
Sourcepub fn with_peer(self, peer: SocketAddr) -> Self
pub fn with_peer(self, peer: SocketAddr) -> Self
Set the address the request arrived from, as the server does.