Skip to main content

Params

Struct Params 

Source
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/jsonbody = Some(parsed), raw_body = original_bytes.
  • application/x-www-form-urlencoded → fields are appended into query (same multimap); body = None, raw_body = original_bytes.
  • any other Content-Type (including application/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::BadRequestbody and raw_body are therefore never both empty by accident.

Implementations§

Source§

impl Params

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get<T: Any + Send + Sync>(&self) -> Option<&T>

Look up a value previously inserted with Params::insert.

Source

pub fn verb(&self) -> Verb

The HTTP verb this request was dispatched with.

Source

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.

Source

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.

Source

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 .

Source

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.

Source

pub fn get_optional(&self, name: &str) -> Option<&str>

Look up a query parameter’s first value; returns None if absent.

Source

pub fn get_int(&self, name: &str) -> Result<i64, WebError>

Parse query parameter name as an i64; 400 if missing or unparsable.

Source

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.

Source

pub fn get_u64(&self, name: &str) -> Result<u64, WebError>

Parse query parameter name as a u64; 400 if missing or unparsable.

Source

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.

Source

pub fn get_u32(&self, name: &str) -> Result<u32, WebError>

Parse query parameter name as a u32; 400 if missing or unparsable.

Source

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.

Source

pub fn get_f64(&self, name: &str) -> Result<f64, WebError>

Parse query parameter name as an f64; 400 if missing or unparsable.

Source

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.

Source

pub fn get_bool(&self, name: &str) -> bool

Read query parameter name as a bool — false when absent, empty, "false", or "0"; true otherwise.

Source

pub fn get_bool_optional(&self, name: &str) -> Option<bool>

Like Params::get_bool, but None when the parameter is absent (rather than false).

Source

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.

Source

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).

Source

pub fn json_body(&self) -> Result<JsonValue, WebError>

The parsed JSON request body, or 400 Bad Request if there wasn’t one.

Source

pub fn check_unexpected(&self, expected: &[&str]) -> Option<Vec<String>>

In strict mode, return any query keys not in expected (so the caller can reject the request); None if every key was expected.

Auto Trait Implementations§

§

impl !Freeze for Params

§

impl !RefUnwindSafe for Params

§

impl !UnwindSafe for Params

§

impl Send for Params

§

impl Sync for Params

§

impl Unpin for Params

§

impl UnsafeUnpin for Params

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more