pub struct Request { /* private fields */ }Expand description
An inbound HTTP request.
Implementations§
Source§impl Request
impl Request
Sourcepub fn new(method: Method, uri: Uri, headers: HeaderMap, body: Bytes) -> Self
pub fn new(method: Method, uri: Uri, headers: HeaderMap, body: Bytes) -> Self
Build a request from its parts. The server calls this; tests should
prefer Request::builder.
Sourcepub fn builder() -> RequestBuilder
pub fn builder() -> RequestBuilder
A fluent builder, mainly for tests.
Sourcepub fn from_http(request: Request<Bytes>) -> Self
pub fn from_http(request: Request<Bytes>) -> Self
Adopt an http::Request whose body has already been buffered.
Sourcepub fn query_string(&self) -> &str
pub fn query_string(&self) -> &str
The raw query string, without the ?.
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Mutable access to the headers — for middleware that normalises them.
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
One header as a string, or None if absent or not valid UTF-8.
Sourcepub fn set_method(&mut self, method: Method)
pub fn set_method(&mut self, method: Method)
Replace the method.
For the one middleware that legitimately needs it: turning a browser
form’s POST with _method=DELETE into a real DELETE before routing
sees it. See
MethodOverride — and note that
it must run before the router, or the route has already been chosen
for the old method.
Sourcepub fn body_string(&self) -> String
pub fn body_string(&self) -> String
The body as UTF-8, lossily.
Sourcepub fn content_type(&self) -> Option<String>
pub fn content_type(&self) -> Option<String>
The Content-Type header, lowercased and without parameters.
Sourcepub fn is_json(&self) -> bool
pub fn is_json(&self) -> bool
Whether the body claims to be JSON. Also true for +json suffixes such
as application/ld+json.
Sourcepub fn expects_json(&self) -> bool
pub fn expects_json(&self) -> bool
Whether the client would rather have JSON back.
True when it sent JSON, when it asked for JSON in Accept, or when it
looks like an XHR. This is what the exception handler consults to decide
between a JSON error body and an HTML error page.
Sourcepub fn body_input(&self) -> &Value
pub fn body_input(&self) -> &Value
The body’s fields as a JSON tree — {} when there is no parseable body.
Sourcepub fn all(&self) -> Value
pub fn all(&self) -> Value
Every input: the query string with the body merged over it.
Route parameters are not included — they are part
of the URL’s shape rather than user input, and mixing them in would let
a query string shadow a route binding. Read them with
route_param.
Sourcepub fn input(&self, key: &str) -> Option<String>
pub fn input(&self, key: &str) -> Option<String>
One input by dotted key, as a string. Body wins over query string.
Sourcepub fn transform_input(&mut self, transform: impl FnOnce(Value) -> Value)
pub fn transform_input(&mut self, transform: impl FnOnce(Value) -> Value)
Rewrite every input through transform.
The seam input-normalising middleware works through: TrimStrings and
ConvertEmptyStringsToNull both take all, reshape it,
and hand it back. Later reads see only the rewritten values.
Sourcepub fn merge_input(&mut self, extra: Value)
pub fn merge_input(&mut self, extra: Value)
Merge extra values over the current inputs, for a middleware that resolves something the handler should read as ordinary input.
Sourcepub fn input_value(&self, key: &str) -> Option<&Value>
pub fn input_value(&self, key: &str) -> Option<&Value>
The raw JSON value at key, for non-scalar inputs.
Sourcepub fn input_was_rewritten(&self) -> bool
pub fn input_was_rewritten(&self) -> bool
Whether input-rewriting middleware has replaced the inputs.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T>
pub fn json<T: DeserializeOwned>(&self) -> Result<T>
Deserialise the JSON body into T.
Fails with a 400 when the body is absent, is not JSON, or does not fit
T — unlike input, which shrugs at a bad body.
Sourcepub fn form<T: DeserializeOwned>(&self) -> Result<T>
pub fn form<T: DeserializeOwned>(&self) -> Result<T>
Deserialise the merged inputs into T.
Every value from a form or query string is a string, so T should
either use string fields or #[serde(deserialize_with = ..)] coercions.
For typed coercion from forms, validate instead.
Sourcepub fn query_as<T: DeserializeOwned>(&self) -> Result<T>
pub fn query_as<T: DeserializeOwned>(&self) -> Result<T>
Deserialise the query string into T.
Sourcepub fn files(&self, field: &str) -> &[UploadedFile]
pub fn files(&self, field: &str) -> &[UploadedFile]
Every file uploaded under field.
Sourcepub fn file(&self, field: &str) -> Option<&UploadedFile>
pub fn file(&self, field: &str) -> Option<&UploadedFile>
The first file uploaded under field.
Every cookie the client sent.
One cookie’s value.
Sourcepub fn bearer_token(&self) -> Option<&str>
pub fn bearer_token(&self) -> Option<&str>
The Authorization: Bearer … token, if present.
Sourcepub fn route_param(&self, name: &str) -> Option<&str>
pub fn route_param(&self, name: &str) -> Option<&str>
A parameter captured by the matched route (/posts/{post}).
Sourcepub fn route_params(&self) -> &HashMap<String, String>
pub fn route_params(&self) -> &HashMap<String, String>
Every captured route parameter.
Sourcepub fn set_route_params(&mut self, params: HashMap<String, String>)
pub fn set_route_params(&mut self, params: HashMap<String, String>)
Replace the captured route parameters. The router calls this on match.
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Per-request attributes, keyed by type: the authenticated user, the matched route, a resolved model binding.
Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Mutable per-request attributes.
Sourcepub fn with_extension<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn with_extension<T: Send + Sync + 'static>(self, value: T) -> Self
Store a per-request attribute, returning self for chaining.