pub struct Params { /* private fields */ }Expand description
Raw parameters from the HTTP request, plus headers, the parsed body, and a typed extensions slot for per-request data that prepare hooks (or middleware) want to attach for handlers to read.
Headers are a multimap, just like query: each lowercased name maps to
every value seen for it, in request order. Scalar accessor
Params::header reads the first value (the common case);
Params::header_all returns every value (for Forwarded, Via, etc.
which can legitimately appear multiple times — e.g. in a proxy chain).
Query parameters are a multimap: each name maps to every value seen
for it, in request order — ?tags=a&tags=b is {"tags": ["a", "b"]}.
Scalar accessors (require, get_u64, …) read the first value;
Params::get_all returns the whole list (this is what backs
Vec<String> handler parameters, so a one-element array works the same
as a many-element one). Repeated keys are what create multiple values;
a comma in a single value (?tags=a,b) is just one value "a,b".
body and raw_body are populated by the server’s Request::to_params
using Content-Type discrimination:
application/json→body = Some(parsed),raw_body = original_bytes.application/x-www-form-urlencoded→ fields are appended intoquery(same multimap);body = None,raw_body = original_bytes.- any other
Content-Type(includingapplication/octet-stream,application/zip, …) →body = None,raw_body = original_bytes. - empty body →
body = None,raw_body = Bytes::new().
A non-empty request body without a Content-Type header is rejected
at ingest with WebError::BadRequest — body and raw_body are
therefore never both empty by accident.
Implementations§
Source§impl Params
impl Params
Sourcepub fn new(
verb: Verb,
query: HashMap<String, Vec<String>>,
body: Option<JsonValue>,
raw_body: Bytes,
headers: HashMap<String, Vec<String>>,
) -> Self
pub fn new( verb: Verb, query: HashMap<String, Vec<String>>, body: Option<JsonValue>, raw_body: Bytes, headers: HashMap<String, Vec<String>>, ) -> Self
Construct a Params from the extracted request pieces. Called by the
framework’s dispatch path; handlers receive an already-built Params.
Sourcepub fn query(&self) -> &HashMap<String, Vec<String>>
pub fn query(&self) -> &HashMap<String, Vec<String>>
The entire query multimap — every parameter name and all its values,
in request order, plus any folded application/x-www-form-urlencoded
body fields.
Use this for “catch the rest” handlers — a search endpoint with
open-ended filters, a request proxy, etc.: declare params: &Params,
mark the controller #[controller(lax)] so strict mode doesn’t reject
the undeclared keys, and read params.query(). Handlers that know
their parameters up front should declare them as typed arguments
instead; this is the escape hatch, not the default.
Sourcepub fn body_bytes(&self) -> &Bytes
pub fn body_bytes(&self) -> &Bytes
Raw bytes of the request body, before any content-type-specific
parsing. Always present (Bytes::new() for empty bodies).
Use this when the handler consumes binary uploads (.uwx, image
blobs, etc.). For JSON bodies, prefer Params::json_body or
macro-extracted typed args — those operate on the parsed value
the framework already produced from the same bytes.
Sourcepub fn insert<T: Any + Send + Sync>(&mut self, value: T) -> Option<T>
pub fn insert<T: Any + Send + Sync>(&mut self, value: T) -> Option<T>
Stash a value for later retrieval. The value is keyed by its type;
inserting a second value of the same type replaces the first.
Typically called from a prepare hook to pass a resolved user (or
other request-scoped state) through to the handler.
Sourcepub fn get<T: Any + Send + Sync>(&self) -> Option<&T>
pub fn get<T: Any + Send + Sync>(&self) -> Option<&T>
Look up a value previously inserted with Params::insert.
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Look up a request header (case-insensitive). Returns the first
value if the header appears more than once; see Params::header_all
for every value.
Sourcepub fn header_all(&self, name: &str) -> &[String]
pub fn header_all(&self, name: &str) -> &[String]
Every value for a request header (case-insensitive), in receipt
order. Empty slice if the header wasn’t present. Use this for headers
that can legitimately appear multiple times — Forwarded, Via,
X-Forwarded-For (when proxies emit one entry per hop), etc.
Sourcepub fn bearer_token(&self) -> Option<&str>
pub fn bearer_token(&self) -> Option<&str>
Convenience: extract a Bearer token from the Authorization header.
Returns None if the header is missing or doesn’t start with Bearer .
Sourcepub fn require(&self, name: &str) -> Result<&str, WebError>
pub fn require(&self, name: &str) -> Result<&str, WebError>
The first value of query parameter name, or a 400 Bad Request if
it’s absent.
Sourcepub fn get_optional(&self, name: &str) -> Option<&str>
pub fn get_optional(&self, name: &str) -> Option<&str>
Look up a query parameter’s first value; returns None if absent.
Sourcepub fn get_int(&self, name: &str) -> Result<i64, WebError>
pub fn get_int(&self, name: &str) -> Result<i64, WebError>
Parse query parameter name as an i64; 400 if missing or unparsable.
Sourcepub fn get_int_optional(&self, name: &str) -> Result<Option<i64>, WebError>
pub fn get_int_optional(&self, name: &str) -> Result<Option<i64>, WebError>
Parse optional query parameter name as an i64; Ok(None) if absent,
400 if present but unparsable.
Sourcepub fn get_u64(&self, name: &str) -> Result<u64, WebError>
pub fn get_u64(&self, name: &str) -> Result<u64, WebError>
Parse query parameter name as a u64; 400 if missing or unparsable.
Sourcepub fn get_u64_optional(&self, name: &str) -> Result<Option<u64>, WebError>
pub fn get_u64_optional(&self, name: &str) -> Result<Option<u64>, WebError>
Parse optional query parameter name as a u64; Ok(None) if absent,
400 if present but unparsable.
Sourcepub fn get_u32(&self, name: &str) -> Result<u32, WebError>
pub fn get_u32(&self, name: &str) -> Result<u32, WebError>
Parse query parameter name as a u32; 400 if missing or unparsable.
Sourcepub fn get_u32_optional(&self, name: &str) -> Result<Option<u32>, WebError>
pub fn get_u32_optional(&self, name: &str) -> Result<Option<u32>, WebError>
Parse optional query parameter name as a u32; Ok(None) if absent,
400 if present but unparsable.
Sourcepub fn get_f64(&self, name: &str) -> Result<f64, WebError>
pub fn get_f64(&self, name: &str) -> Result<f64, WebError>
Parse query parameter name as an f64; 400 if missing or unparsable.
Sourcepub fn get_f64_optional(&self, name: &str) -> Result<Option<f64>, WebError>
pub fn get_f64_optional(&self, name: &str) -> Result<Option<f64>, WebError>
Parse optional query parameter name as an f64; Ok(None) if absent,
400 if present but unparsable.
Sourcepub fn get_bool(&self, name: &str) -> bool
pub fn get_bool(&self, name: &str) -> bool
Read query parameter name as a bool — false when absent, empty,
"false", or "0"; true otherwise.
Sourcepub fn get_bool_optional(&self, name: &str) -> Option<bool>
pub fn get_bool_optional(&self, name: &str) -> Option<bool>
Like Params::get_bool, but None when the parameter is absent
(rather than false).
Sourcepub fn get_all(&self, name: &str) -> Result<Vec<String>, WebError>
pub fn get_all(&self, name: &str) -> Result<Vec<String>, WebError>
All values of query parameter name, in request order (empty if the
name wasn’t present). Backs Vec<String> handler parameters.
Sourcepub fn get_all_optional(&self, name: &str) -> Option<Vec<String>>
pub fn get_all_optional(&self, name: &str) -> Option<Vec<String>>
All values of query parameter name; None if the name wasn’t present
at all (vs. Some(vec![]), which urlencoding can’t actually produce).