#[non_exhaustive]pub struct Request { /* private fields */ }Expand description
HTTP Request wrapper providing convenient access to request data.
Created by the routes! macro from raw request-data. Provides:
- Path parameters extracted from route patterns (e.g.,
/users/{id}) - Query string parsing
- Header access (case-insensitive)
- Body access (raw bytes, text, or parsed via external JSON)
§Example
fn get_user(req: &Request) -> Response {
let id = req.param("id").unwrap_or("0");
let page = req.query("page").unwrap_or("1");
let auth = req.header("authorization");
// ...
}§Implementation Notes
This struct stores headers with an index-based lookup optimization:
headers: Original header pairs forheaders()iterationheader_index: Maps lowercase keys to indices inheadersfor O(1) lookups
This avoids cloning header values while providing:
- O(1) header lookups via
header()andheader_all() - Original header iteration via
headers()
Implementations§
Source§impl Request
impl Request
Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
Full request path including query string (e.g., “/users/123?page=1”).
Sourcepub fn path_without_query(&self) -> &str
pub fn path_without_query(&self) -> &str
Just the path portion without query string (e.g., “/users/123”).
Sourcepub fn param(&self, name: &str) -> Option<&str>
pub fn param(&self, name: &str) -> Option<&str>
Get a path parameter extracted from the route pattern.
For route /users/{id} matching path /users/123, param("id") returns Some("123").
Sourcepub fn query(&self, name: &str) -> Option<&str>
pub fn query(&self, name: &str) -> Option<&str>
Get the first query parameter value from the URL.
For path /users?page=2&limit=10, query("page") returns Some("2").
For multiple values with same key, use query_all().
Sourcepub fn query_all(&self, name: &str) -> &[String]
pub fn query_all(&self, name: &str) -> &[String]
Get all query parameter values for a key.
HTTP allows multiple query params with the same name (e.g., ?ids=1&ids=2&ids=3).
This returns all values for such parameters.
// For URL: /search?tag=rust&tag=wasm&tag=http
let tags = req.query_all("tag");
assert_eq!(tags, &["rust", "wasm", "http"]);Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Get the first header value by name (case-insensitive).
Uses pre-normalized HashMap for O(1) lookup. Avoids allocation when
the header name is already lowercase (common case).
For headers with multiple values (e.g., Set-Cookie), use header_all().
let content_type = req.header("content-type");
let auth = req.header("Authorization"); // Same as "authorization"Sourcepub fn trace_id(&self) -> Option<&str>
pub fn trace_id(&self) -> Option<&str>
Get the trace ID from the incoming request.
Returns the value of the x-trace-id header if present.
Use this with ClientRequest::with_trace_id() to propagate
trace context to outgoing HTTP calls.
let response = fetch!(GET "https://api.example.com/data")
.with_trace_id(req.trace_id())
.send_with(&handler)?;Sourcepub fn header_all(&self, name: &str) -> Vec<&str>
pub fn header_all(&self, name: &str) -> Vec<&str>
Get all values for a header (case-insensitive).
HTTP allows multiple headers with the same name (e.g., Set-Cookie, Accept). This returns all values for such headers.
let cookies = req.header_all("set-cookie");
for cookie in &cookies {
println!("Cookie: {}", cookie);
}Sourcepub fn headers(&self) -> &[(String, String)]
pub fn headers(&self) -> &[(String, String)]
Get all headers as name-value pairs.
Returns headers in their original form (before normalization).
Sourcepub fn body(&self) -> Option<&[u8]>
pub fn body(&self) -> Option<&[u8]>
Raw request body bytes.
Returns the raw bytes of the request body, or None if no body was provided.
§Returns
Some(&[u8])- The raw body bytesNone- No body in request
Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
Request body as UTF-8 text.
§Returns
Some(&str)- Body successfully decoded as UTF-8None- No body, or body is not valid UTF-8
Sourcepub fn content_type(&self) -> Option<&str>
pub fn content_type(&self) -> Option<&str>
Content-Type header value.
§Returns
Some(&str)- The Content-Type header valueNone- No Content-Type header present
Sourcepub fn accepts(&self, mime: &str) -> bool
pub fn accepts(&self, mime: &str) -> bool
Check if client accepts a content type (via Accept header).
Performs a simple case-insensitive substring match against the Accept header.
Does not parse q-values; returns true if the MIME type is present at all.
// Accept: text/html, application/json
req.accepts("json") // true
req.accepts("html") // true
req.accepts("xml") // falseSourcepub fn form(&self, name: &str) -> Option<&str>
pub fn form(&self, name: &str) -> Option<&str>
Get the first form field value from a form-urlencoded body.
Parses application/x-www-form-urlencoded body data.
For multiple values with same key, use form_all().
§Returns
Some(&str)- The decoded field valueNone- Field not present, no body, or body is not valid UTF-8
§Examples
// Body: name=Alice&email=alice%40example.com
let name = req.form("name"); // Some("Alice")
let email = req.form("email"); // Some("alice@example.com")Sourcepub fn form_all(&self, name: &str) -> &[String]
pub fn form_all(&self, name: &str) -> &[String]
Get all form field values for a key from a form-urlencoded body.
// Body: tags=rust&tags=wasm&tags=http
let tags = req.form_all("tags"); // &["rust", "wasm", "http"]