Raw path parameters
PathParams<T>
is a high-level interface: it bundles together compile-time checks,
extraction and parsing.
If you want to opt out of all those utilities, reach for RawPathParams
.
RawPathParams
is a lower-level interface1: it gives you access to the dynamic
path segments as they appear right after extraction.
It doesn't perform percent-decoding nor deserialization.
Injection
RawPathParams
is a framework primitive,
you don't have to register a constructor to inject it.
use pavex::http::StatusCode;
use pavex::request::path::RawPathParams;
pub fn handler(params: &RawPathParams) -> StatusCode {
for (name, value) in params.iter() {
println!("`{name}` was set to `{}`", value.as_str());
}
// [...]
}
What does "raw" mean?
Path parameters must comply with the restriction of the URI specification:
you can only use a limited set of characters.
If you want to use a character not allowed in a URI, you must percent-encode it.
For example, if you want to send 123 456
as a route parameter, you must encode it as
123%20456
where %20
is a percent-encoded whitespace.
RawPathParams
gives you access to the raw route parameters, i.e. the route parameters
as they are extracted from the URL, before any kind of processing has taken
place.
In particular, RawPathParams
does not perform any percent-decoding.
If you send a request to /address/123%20456/home/789
, the RawPathParams
for
/address/:address_id/home/:home_id
will contain the following key-value pairs:
address_id
:123%20456
home_id
:789
address_id
is not 123 456
because RawPathParams
does not perform percent-decoding!
Therefore %20
is not interpreted as a space character.
There are situations where you might want to work with the raw route parameters, but
most of the time you'll want to use PathParams
instead—it performs percent-decoding
and deserialization for you.
Allocations
RawPathParams
tries to avoid heap memory allocations.
Parameter names are borrowed from the server routing machinery.
Parameter values are borrowed from the raw path of the incoming request.
You might have to allocate when you perform percent-decoding.
-
PathParams<T>
is built on top ofRawPathParams
. ↩