pub fn encode_query_param(s: impl AsRef<str>) -> StringExpand description
Encodes a query parameter name or value using + for spaces.
This function follows the application/x-www-form-urlencoded encoding format,
which is commonly used in HTML form submissions. The key difference from
encode_uri_component is that spaces are encoded as + instead of %20.
§Preserved Characters
The following characters are passed through unchanged:
- Unreserved:
A-Z,a-z,0-9,-,_,.,!,~,*,',(,) - Reserved:
;,,,/,?,:,@,&,=,+,$,#
Spaces are converted to +, and all other characters are percent-encoded.
§Use Cases
Use this function when building query strings that will be submitted as form data
or when you want the more compact + encoding for spaces.
§Examples
use uri_encode::encode_query_param;
// Spaces become +
assert_eq!(encode_query_param("hello world"), "hello+world");
// Building a query string
let name = "John Doe";
let city = "New York";
let query = format!("name={}&city={}", encode_query_param(name), encode_query_param(city));
assert_eq!(query, "name=John+Doe&city=New+York");