pub struct Request {
pub method: Method,
pub uri: Uri,
pub headers: HeaderMap,
pub body: Bytes,
pub params: Params,
pub query: Query,
pub extensions: Extensions,
pub remote_addr: Option<SocketAddr>,
pub matched_path: Option<Arc<str>>,
pub secure: bool,
/* private fields */
}Expand description
HTTP Request wrapper
Fields§
§method: Method§uri: Uri§headers: HeaderMap§body: Bytes§params: Params§query: Query§extensions: ExtensionsTyped per-request data; middleware can attach values for handlers (authenticated user, request id, …).
remote_addr: Option<SocketAddr>Address of the connected peer (the proxy when behind one)
matched_path: Option<Arc<str>>The route pattern that matched, e.g. /users/{id}; None when no route
matched. Use it, not the raw path, as a metrics label or log field: it
stays the same for every id.
secure: boolThe request arrived over TLS (App::tls). Behind a TLS-terminating
proxy this is false; see secure::RequireHttps::trust_forwarded_proto.
Implementations§
Source§impl Request
impl Request
Sourcepub fn multipart(&self) -> Result<Multipart>
pub fn multipart(&self) -> Result<Multipart>
Parse a multipart/form-data body with the default MultipartLimits
415 for other content types, 400 for malformed bodies.
pub fn multipart_with(&self, limits: &MultipartLimits) -> Result<Multipart>
Source§impl Request
impl Request
Sourcepub fn request_id(&self) -> Option<&str>
pub fn request_id(&self) -> Option<&str>
Id assigned by RequestIdLayer, if installed
Source§impl Request
impl Request
pub fn new(method: Method, uri: Uri, headers: HeaderMap, body: Bytes) -> Self
Sourcepub fn accepts<'a>(&self, offered: &[&'a str]) -> Option<&'a str>
pub fn accepts<'a>(&self, offered: &[&'a str]) -> Option<&'a str>
Which of offered media types the client prefers according to Accept
(None when it accepts none of them), see crate::negotiate
Sourcepub fn accepts_language<'a>(&self, offered: &[&'a str]) -> Option<&'a str>
pub fn accepts_language<'a>(&self, offered: &[&'a str]) -> Option<&'a str>
Which of offered languages the client prefers according to Accept-Language
Sourcepub fn body_stream(&mut self) -> Result<BodyStream>
pub fn body_stream(&mut self) -> Result<BodyStream>
The body as a stream, see crate::upload. On routes registered with
App::stream_body nothing was buffered; on other routes the already
buffered body is served as a single chunk, so a handler works either way.
The stream can be taken once.
Sourcepub fn csp_nonce(&self) -> Option<&str>
pub fn csp_nonce(&self) -> Option<&str>
Nonce for inline scripts and styles, when the
SecurityHeaders policy uses the
{nonce} placeholder
Sourcepub fn client_certificate(&self) -> Option<&[u8]>
pub fn client_certificate(&self) -> Option<&[u8]>
DER of the leaf certificate the client presented during the TLS
handshake (mutual TLS, feature tls); None for anonymous clients and
plain HTTP. The certificate has already been verified against the CAs
given to TlsConfig::client_auth; check which verified client it is
(fingerprint or subject) before authorizing anything.
Sourcepub fn state<T: Send + Sync + 'static>(&self) -> Result<&T>
pub fn state<T: Send + Sync + 'static>(&self) -> Result<&T>
Shared application state registered with App::state
Answers 500 when no value of type T was registered: that is a
programming error, not something the client can fix.
Sourcepub fn shutdown_signal(&self) -> Shutdown
pub fn shutdown_signal(&self) -> Shutdown
Signal that fires when the server begins shutting down
Long-lived handlers (SSE streams, polling loops) select on it to end
promptly: tokio::select! { _ = req.shutdown_signal().wait() => break, ... }.
Sourcepub fn state_arc<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>
pub fn state_arc<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>
An owned handle to the same state, for when the value must outlive a
borrow of the request (it is moved into middleware next.run(req), a
spawned task, …)
Sourcepub fn try_state<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn try_state<T: Send + Sync + 'static>(&self) -> Option<&T>
Like Request::state, None when the type is not registered
Get cookies from the request
Sourcepub fn form<T: DeserializeOwned>(&self) -> Result<T>
pub fn form<T: DeserializeOwned>(&self) -> Result<T>
Parse an application/x-www-form-urlencoded body into a struct
Answers 415 for other content types and 400 for values that do not
fit the target type.
Sourcepub fn form_data(&self) -> Result<Query>
pub fn form_data(&self) -> Result<Query>
The same body as decoded key/value access (+ and %XX decoded)
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T>
pub fn json<T: DeserializeOwned>(&self) -> Result<T>
Parse JSON body
Source§impl Request
impl Request
Sourcepub fn session(&self) -> Result<&Session>
pub fn session(&self) -> Result<&Session>
The session of this request (needs the SessionLayer middleware;
answers 500 otherwise)
Source§impl Request
impl Request
Sourcepub fn trace(&self) -> Option<&TraceContext>
pub fn trace(&self) -> Option<&TraceContext>
Trace ids assigned by TraceLayer, if installed