Example of Vec<String> in Query? #1719
-
I looked at #434 but still have no idea how I'm supposed to make something like this work in 0.6.2: #[derive(Deserialize)]
pub struct Q {
a: Vec<String>,
}
pub async fn handler(
Query(payload): Query<Q>,
) -> Result {
... No matter if I try
Is there an example somewhere I'm missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
There is no standard way to encode collections as query params, therefore With that said you can use |
Beta Was this translation helpful? Give feedback.
-
While the answer works, I'm noticing some intermittent behaviour when an array of one item is passed. Take the following simplified example. #[derive(Debug, Deserialize)]
pub(crate) Params {
pub(crate): some_flag: Option<bool>,
#[serde(flatten)]
pub(crate): filters: Filters,
}
#[derive(Debug, Deserialize)]
pub(crate) Filters {
#[serde(rename = "filter")]
pub(crate) filters: Option<Vec<String>>,
} Passing
Not sure if this is expected behaviour? |
Beta Was this translation helpful? Give feedback.
There is no standard way to encode collections as query params, therefore
serde_url_encoded
doesn't support it. That is the parser axum'sQuery
uses.With that said you can use
axum_extra::extract::Query
instead which expects params the way HTML forms encodes them. Example here https://github.com/tokio-rs/axum/blob/main/axum-extra/src/extract/query.rs#L147. You can also useserde_qs
which does support thea[]
syntax.