In Salvo, user request data can be obtained through the Request
struct:
Request
is a struct representing HTTP requests, providing comprehensive request-handling capabilities:
extract
methodQuery parameters can be obtained using get_query
:
Form data can be retrieved using get_form
, which is an asynchronous function:
Request
provides multiple methods to parse data into strongly typed structures:
parse_params
: Parses router params into a specific type.parse_queries
: Parses URL queries into a specific type.parse_headers
: Parses HTTP headers into a specific type.parse_json
: Parses the HTTP body as JSON into a specific type.parse_form
: Parses the HTTP body as form data into a specific type.parse_body
: Parses the HTTP body based on the content-type
into a specific type.extract
: Combines different data sources to extract a specific type.A custom serde::Deserializer
is used to extract data from structures like HashMap<String, String>
and HashMap<String, Vec<String>>
into specific types.
For example, URL queries
are extracted as a MultiMap, which is similar to HashMap<String, Vec<String>>
. If the requested URL is http://localhost/users?id=123&id=234
, and the target type is:
The first id=123
will be parsed, while id=234
is discarded:
If the target type is:
Both id=123
and id=234
will be parsed:
The framework includes built-in request parameter extractors, which significantly simplify HTTP request handling.
To use these extractors, enable the "oapi"
feature in your Cargo.toml
:
Then import the extractors:
Extracts JSON data from the request body and deserializes it into a specified type.
Extracts form data from the request body and deserializes it into a specified type.
Extracts a specific value from request cookies.
Extracts a specific value from request headers.
Extracts parameters from the URL path.
Extracts parameters from the URL query string.
Multiple data sources can be combined to extract a specific type. First, define a custom type:
Then, in a handler, extract the data as follows:
Alternatively, pass the type directly as a function parameter:
The data type definition is highly flexible, allowing for nested structures:
For a practical example, see: extract-nested.
#[salvo(extract(flatten))]
vs.#[serde(flatten)]
If the nested struct Nested<'a>
does not share fields with its parent, #[serde(flatten)]
can be used. Otherwise, #[salvo(extract(flatten))]
is required.
#[salvo(extract(source(parse)))]
The source
attribute can also include a parse
parameter to specify a custom parsing method. If omitted, the parsing method is determined by the request's content-type
(e.g., Form
data is parsed as MultiMap
, while JSON payloads are parsed as JSON). This parameter is rarely needed but can enable special functionality.
Here, the request sends form data, but one field contains JSON text. By specifying parse = "json"
, the string is parsed as JSON.
For the latest and most detailed information, refer to the crates.io API documentation.
Category | Method | Description |
---|---|---|
Request Info | uri()/uri_mut()/set_uri() | URI operations |
method()/method_mut() | HTTP method operations | |
version()/version_mut() | HTTP version operations | |
scheme()/scheme_mut() | Protocol scheme operations | |
remote_addr()/local_addr() | Address information | |
Headers | headers()/headers_mut() | Retrieve all headers |
header<T>()/try_header<T>() | Retrieve and parse a specific header | |
add_header() | Add a header | |
content_type()/accept() | Retrieve content type/accept headers | |
Parameters | params()/param<T>() | Path parameter operations |
queries()/query<T>() | Query parameter operations | |
form<T>()/form_or_query<T>() | Form data operations | |
Request Body | body()/body_mut() | Retrieve the request body |
replace_body()/take_body() | Modify/extract the request body | |
payload()/payload_with_max_size() | Retrieve raw data | |
File Handling | file()/files()/all_files() | Retrieve uploaded files |
first_file() | Retrieve the first file | |
Data Parsing | extract<T>() | Unified data extraction |
parse_json<T>()/parse_form<T>() | Parse JSON/form data | |
parse_body<T>() | Smart request body parsing | |
parse_params<T>()/parse_queries<T>() | Parse parameters/queries | |
Special Features | cookies()/cookie() | Cookie operations (requires cookie feature) |
extensions()/extensions_mut() | Extension data storage | |
set_secure_max_size() | Set secure size limits |