Skip to content

Commit 1ee9d5b

Browse files
committed
add doc example for request query
1 parent 2c82985 commit 1ee9d5b

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

src/request.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,54 @@ impl<State> Request<State> {
280280
.parse()
281281
}
282282

283-
/// Get the URL querystring.
283+
/// Parse the URL query component into a struct, using [serde_qs](serde_qs). To get the entire
284+
/// query as an unparsed string, use `request.url().query()`
285+
///
286+
/// ```rust
287+
/// # fn main() -> Result<(), std::io::Error> { async_std::task::block_on(async {
288+
/// use tide::prelude::*;
289+
/// let mut app = tide::new();
290+
///
291+
/// #[derive(Deserialize)]
292+
/// #[serde(default)]
293+
/// struct Page {
294+
/// size: u8,
295+
/// offset: u8,
296+
/// }
297+
/// impl Default for Page {
298+
/// fn default() -> Self {
299+
/// Self {
300+
/// size: 25,
301+
/// offset: 0,
302+
/// }
303+
/// }
304+
/// }
305+
/// app.at("/pages").post(|mut req: tide::Request<()>| async move {
306+
/// let page: Page = req.query()?;
307+
/// Ok(format!("page {}, with {} items", page.offset, page.size))
308+
/// });
309+
///
310+
/// # if false {
311+
/// app.listen("localhost:8000").await?;
312+
/// # }
313+
///
314+
/// // $ curl localhost:8000/pages
315+
/// // page 0, with 25 items
316+
///
317+
/// // $ curl localhost:8000/pages?offset=1
318+
/// // page 1, with 25 items
319+
///
320+
/// // $ curl localhost:8000/pages?offset=2&size=50
321+
/// // page 2, with 50 items
322+
///
323+
/// // $ curl localhost:8000/pages?size=5000
324+
/// // failed with reason: number too large to fit in target type
325+
///
326+
/// // $ curl localhost:8000/pages?size=all
327+
/// // failed with reason: invalid digit found in string
328+
/// # Ok(()) })}
329+
/// ```
330+
284331
pub fn query<T: serde::de::DeserializeOwned>(&self) -> crate::Result<T> {
285332
self.req.query()
286333
}

0 commit comments

Comments
 (0)