The 'Request' struct
The Request struct is the main extractor for working with HTTP requests in Sword web controllers.
API Reference
Accessing the request extensions
Returns the request extensions, where layers and interceptors can store values.
pub fn extensions(&self) -> &Extensions
pub fn extensions_mut(&mut self) -> &mut ExtensionsWhen to use it
- To read or write state shared with layers and interceptors.
Getting the request URI
Returns a reference to the full request URI.
pub fn uri(&self) -> &UriReturns
- The request URI, including the path and query string.
Getting the HTTP version
pub fn version(&self) -> VersionReturns
- The HTTP version of the request.
Getting the HTTP method
pub fn method(&self) -> &MethodReturns
- The request's HTTP method.
Getting a request header
pub fn header(&self, key: &str) -> Option<&str>Returns
- The header value if it exists and is valid UTF-8.
Getting all request headers
pub fn headers(&self) -> &HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMapReturns
- An immutable and a mutable reference to the
HeaderMap, respectively.
Adding or replacing a request header
pub fn set_header(&mut self, name: impl Into<String>, value: impl Into<String>) -> Result<(), RequestError>Returns
Ok(())if the header was inserted or replaced.Err(RequestError)if the name or value is invalid.
Getting a route parameter
pub fn param<T>(&self, key: &str) -> Result<T, RequestError>
where
T: FromStr,
T::Err: Display,Parameters
Tis the type to parse the route parameter into.keyis the name of the route parameter to extract.
Returns
Ok(T)if the parameter exists and parses.Err(RequestError)if it is missing or cannot be parsed.
When to use it
- Route parameters such as
/users/{id}.
Getting all route parameters
pub fn params(&self) -> &RawPathParamsReturns
- The raw path parameters (percent-decoded, not deserialized) as
RawPathParams.
When to use it
- When you need to iterate over every path param without knowing their names in advance. For a specific one, use
param::<T>().
Getting the request body
pub fn body<T: DeserializeOwned>(&self) -> Result<T, RequestError>Parameters
Tis the type to deserialize the JSON body into. It must implementDeserializeOwned.
Returns
Ok(T)if the body is valid JSON compatible withT.Err(RequestError)for an empty body, an invalid media type, or a deserialization failure.
When to use it
- JSON requests in
POST,PUT, andPATCHendpoints.
Getting the request query string
pub fn query<T: DeserializeOwned>(&self) -> Result<Option<T>, RequestError>Parameters
Tis the type to deserialize the query string into. It must implementDeserializeOwned.
Returns
Ok(Some(T))if the query string is valid.Ok(None)if there is no query string.Err(RequestError)if the query exists but cannot be deserialized.
Accessing request cookies
pub fn cookies(&self) -> Result<&Cookies, JsonResponse>Returns
Ok(&Cookies)if the cookie layer is available.Err(JsonResponse)if cookies cannot be extracted.
Getting the 'Authorization' header
Direct access to the request's Authorization header.
pub fn authorization(&self) -> Option<&str>Returns
- The
Authorizationheader value, orNoneif it is missing or not valid UTF-8.
Getting the 'User-Agent' header
Direct access to the request's User-Agent header.
pub fn user_agent(&self) -> Option<&str>Returns
- The
User-Agentheader value, orNoneif it is missing or not valid UTF-8.
Getting the client IP
Direct access to the first value of the request's X-Forwarded-For header.
pub fn ip(&self) -> Option<&str>Returns
- The first
X-Forwarded-Forvalue as plain text, orNoneif it is missing.
Getting all client IPs
Direct access to the request's X-Forwarded-For header, which can contain multiple IPs.
pub fn ips(&self) -> Option<Vec<&str>>Returns
- The comma-separated IPs from
X-Forwarded-For, orNoneif the header is missing.
Getting the request protocol
Direct access to the request's X-Forwarded-Proto header.
pub fn protocol(&self) -> &strReturns
- The
X-Forwarded-Protovalue, or"http"by default.
Getting the 'Content-Type' header
Direct access to the request's Content-Type header.
pub fn content_type(&self) -> Option<&str>Returns
- The
Content-Typevalue, orNoneif it is missing or not valid UTF-8.
Getting the 'Content-Length' header
pub fn content_length(&self) -> Option<u64>Returns
Content-Lengthparsed asu64, orNoneif it is missing or not a valid number.
Getting the request ID
pub fn id(&self) -> StringReturns
- The request ID from
RequestIdLayeras a UUID v4. "unknown"if it is not available.
Moving to the next interceptor
This method continues the interceptor chain. Use it only inside OnRequest* implementations, not in regular web handlers.
pub async fn next(self) -> WebInterceptorResult
