Request
In Salvo, user request data can be obtained through Request:
Quick Overview
Request is a struct representing an HTTP request, providing comprehensive request handling capabilities:
- Operates on basic attributes (URI, method, version)
- Handles request headers and Cookies
- Parses various parameters (path, query, form)
- Supports request body processing and file uploads
- Offers multiple data parsing methods (JSON, form, etc.)
- Implements unified type-safe data extraction via the extract method
Getting Query Parameters
Query parameters can be obtained via get_query:
Getting Form Data
Form data can be obtained via get_form. This function is asynchronous:
Getting JSON Deserialized Data
Extracting Request Data
Request provides multiple methods to parse this data into strongly-typed structures.
parse_params: Parses the request's router params into a specific data type.parse_queries: Parses the request's URL queries into a specific data type.parse_headers: Parses the request's HTTP headers into a specific data type.parse_json: Parses the data in the HTTP body part of the request as JSON format into a specific type.parse_form: Parses the data in the HTTP body part of the request as a Form into a specific type.parse_body: Parses the data in the HTTP body part into a specific type based on the request'scontent-type.extract: Can merge different data sources to parse out a specific type.
Parsing Principle
Here, a custom serde::Deserializer is used to extract data from structures like HashMap<String, String> and HashMap<String, Vec<String>> into specific data types.
For example: URL queries are actually extracted as a MultiMap type. MultiMap can be thought of as a data structure similar to HashMap<String, Vec<String>>. If the requested URL is http://localhost/users?id=123&id=234, and our target type is:
Then the first id=123 will be parsed, and id=234 will be discarded:
If our provided type is:
Then both id=123&id=234 will be parsed:
Built-in Extractors
The framework includes built-in request parameter extractors. These extractors can significantly simplify code for handling HTTP requests.
To use them, you need to add the "oapi" feature in your Cargo.toml
Then you can import the extractors:
JsonBody
Used to extract JSON data from the request body and deserialize it into a specified type.
FormBody
Extracts form data from the request body and deserializes it into a specified type.
CookieParam
Extracts a specific value from the request's Cookies.
HeaderParam
Extracts a specific value from the request headers.
PathParam
Extracts parameters from the URL path.
QueryParam
Extracts parameters from the URL query string.
Advanced Usage
You can merge multiple data sources to parse a specific type. First, define a custom type, for example:
Then in a Handler, you can get the data like this:
You can even pass the type directly as a function parameter, like this:
Data type definitions offer considerable flexibility, even allowing parsing into nested structures as needed:
For a concrete example, see: extract-nested.
#[salvo(extract(flatten))] VS #[serde(flatten)]
If in the above example Nested<'a> does not have fields with the same names as the parent, you can use #[serde(flatten)]. Otherwise, you need to use #[salvo(extract(flatten))].
#[salvo(extract(source(parse)))]
You can also add a parse parameter to source to specify a particular parsing method. If this parameter is not specified, parsing is determined based on the Request information. For a Form body, it's parsed as MultiMap; for a JSON payload, it's parsed as JSON. Generally, you don't need to specify this parameter. In rare cases, specifying it can enable special functionality.
For instance, here the actual request sends a Form, but the value of a certain field is a piece of JSON text. By specifying parse, this string can be parsed in JSON format.