I can't visit my page without every single query parameter defined
"Query deserialize error: missing field"
Add a way to make these optional, and/or add the ability to add defaults if they are not in the query
If this is already possible, add it to the documentation
found out you can use Option
How did you wind up doing this? I have the same question. I've tried having my handler function take an Option<Query<String>> as well as Query<Option<String>>, but I haven't figured out how to make it work. With the former, the param is just ignored. With the latter, I get:
Query deserialize error: invalid type: map, expected option
Edit: sorry for the noise. I misunderstood the interface. For anyone looking for this in the future: you've got to use Query<SomeStruct>, where SomeStruct derives Serialize, and then the fields in the struct can themselves be Options. Example:
#[derive(Deserialize)]
struct ListQueryParams {
marker: Option<String>,
limit: Option<usize>
}
async fn list(params: Query<ListQueryParams>)
-> Result<HttpResponse, SomeError>
{
...
}
In this case, both marker and limit are optional query parameters.
I used a couple of optional fields so I put them in a struct with optional fields, that way I'm not passing an Option but a struct, the fields are the Options
struct MyQuery {
pub field_a: Option<String>,
pub field_b: Option<i32>,
}
Then in the handler
web::Query<MyQuery>
Works like a charm, I'm fairly new at this so if there is a better way, I'm interested :)
To expand on the points made, the Query struct is used to extract the key-value pairs from the query string and uses Serde to do that.
If you want to extract the query-string in its entirety as a string then use req.query_string().
If you're after arbitrary field names then Query<HashMap<String, String>> will probably work for you.
Most helpful comment
To expand on the points made, the
Querystruct is used to extract the key-value pairs from the query string and uses Serde to do that.If you want to extract the query-string in its entirety as a string then use
req.query_string().If you're after arbitrary field names then
Query<HashMap<String, String>>will probably work for you.