Serialize a vec with serde_urlencoded

โš“ Rust    ๐Ÿ“… 2026-02-13    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

Hi,

I try do send an http request with the reqwest crate. This request need query parameters so I build them with a struct. Reqwest serialize the struct with the help of serde_urlencoded. But this serializer doesnโ€™t support Vectors. Is there a way to tell serde to use a custom function to serialize a Vec ?

#[derive(Debug, Serialize)]
pub enum Direction {
    Ascending,
    Descending,
}

#[derive(Debug, Serialize)]
pub struct Query {
  pub foo: Option<bool>,
  pub bar: Vec<Direction>,
}

fn build_request() -> RequestBuilder {
  let query = Query {
    foo: Some(true),
    bar: vec![Direction::Ascending, Direction::Descending],
  };

  Client::builder().build()?
    .get("/baz")
    .query(&query)
}

query struct should be serialized to "?foo=true&bar=Ascending,Descending

3 posts - 2 participants

Read full topic

๐Ÿท๏ธ Rust_feed