@@ -615,31 +615,32 @@ impl Request {
615
615
///
616
616
/// # Examples
617
617
///
618
- /// ```no_run
619
- /// # #[async_std::main]
620
- /// # async fn main() -> http_types::Result<()> {
621
- /// use http_types::convert::{Deserialize, Serialize};
618
+ /// ```
619
+ /// use http_types::convert::Deserialize;
622
620
/// use http_types::{Method, Request, Url};
621
+ /// use std::collections::HashMap;
623
622
///
624
- /// #[derive(Serialize, Deserialize)]
623
+ /// #[derive(Deserialize)]
625
624
/// struct Index {
626
625
/// page: u32,
626
+ /// selections: HashMap<String, String>,
627
627
/// }
628
628
///
629
629
/// let req = Request::new(
630
630
/// Method::Get,
631
- /// Url::parse("https://httpbin.org/get?page=2").unwrap(),
631
+ /// Url::parse("https://httpbin.org/get?page=2&selections[width]=narrow&selections[height]=tall ").unwrap(),
632
632
/// );
633
- /// let Index { page } = req.query()? ;
633
+ /// let Index { page, selections } = req.query().unwrap() ;
634
634
/// assert_eq!(page, 2);
635
- /// # Ok(()) }
635
+ /// assert_eq!(selections["width"], "narrow");
636
+ /// assert_eq!(selections["height"], "tall");
636
637
/// ```
637
638
pub fn query < T : serde:: de:: DeserializeOwned > ( & self ) -> crate :: Result < T > {
638
639
// Default to an empty query string if no query parameter has been specified.
639
640
// This allows successful deserialisation of structs where all fields are optional
640
641
// when none of those fields has actually been passed by the caller.
641
642
let query = self . url ( ) . query ( ) . unwrap_or ( "" ) ;
642
- serde_urlencoded :: from_str ( query) . map_err ( |e| {
643
+ serde_qs :: from_str ( query) . map_err ( |e| {
643
644
// Return the displayable version of the deserialisation error to the caller
644
645
// for easier debugging.
645
646
crate :: Error :: from_str ( StatusCode :: BadRequest , format ! ( "{}" , e) )
@@ -650,29 +651,28 @@ impl Request {
650
651
///
651
652
/// # Examples
652
653
///
653
- /// ```no_run
654
- /// # #[async_std::main]
655
- /// # async fn main() -> http_types::Result<()> {
656
- /// use http_types::convert::{Deserialize, Serialize};
654
+ /// ```
655
+ /// use http_types::convert::Serialize;
657
656
/// use http_types::{Method, Request, Url};
657
+ /// use std::collections::HashMap;
658
658
///
659
- /// #[derive(Serialize, Deserialize )]
659
+ /// #[derive(Serialize)]
660
660
/// struct Index {
661
661
/// page: u32,
662
+ /// topics: Vec<&'static str>,
662
663
/// }
663
664
///
664
- /// let query = Index { page: 2 };
665
+ /// let query = Index { page: 2, topics: vec!["rust", "crabs", "crustaceans"] };
665
666
/// let mut req = Request::new(
666
667
/// Method::Get,
667
- /// Url::parse("https://httpbin.org/get?page=2 ").unwrap(),
668
+ /// Url::parse("https://httpbin.org/get").unwrap(),
668
669
/// );
669
- /// req.set_query(&query)?;
670
- /// assert_eq!(req.url().query(), Some("page=2"));
671
- /// assert_eq!(req.url().as_str(), "https://httpbin.org/get?page=2");
672
- /// # Ok(()) }
670
+ /// req.set_query(&query).unwrap();
671
+ /// assert_eq!(req.url().query(), Some("page=2&topics[0]=rust&topics[1]=crabs&topics[2]=crustaceans"));
673
672
/// ```
674
- pub fn set_query ( & mut self , query : & ( impl Serialize + ?Sized ) ) -> crate :: Result < ( ) > {
675
- let query = serde_urlencoded:: to_string ( query) ?;
673
+ pub fn set_query ( & mut self , query : & impl Serialize ) -> crate :: Result < ( ) > {
674
+ let query = serde_qs:: to_string ( query)
675
+ . map_err ( |e| crate :: Error :: from_str ( StatusCode :: BadRequest , format ! ( "{}" , e) ) ) ?;
676
676
self . url . set_query ( Some ( & query) ) ;
677
677
Ok ( ( ) )
678
678
}
0 commit comments