Skip to content

Commit 46ed55a

Browse files
committed
doc: add example & more to req.query()
Adds information similar to that of `req.param()` for `req.query()`.
1 parent 28e5904 commit 46ed55a

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/request.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,50 @@ impl<State> Request<State> {
266266
Ok(serde_json::from_slice(&body_bytes).map_err(|_| std::io::ErrorKind::InvalidData)?)
267267
}
268268

269-
/// Get the URL querystring.
269+
/// Extract and parse a query string parameter by name.
270+
///
271+
/// Returns the results of parsing the parameter according to the inferred
272+
/// output type `T`, which must have the `serde::Deserialize` trait and be
273+
/// able to have the `'de` lifetime.
274+
///
275+
/// The name should *not* include any of the key-value formatting
276+
/// characters (`?`, `=`, or `&`).
277+
///
278+
/// If no query string exists on the uri, this method will default to
279+
/// parsing an empty string so that deserialization can be succesful to
280+
/// structs where all fields are optional.
281+
///
282+
/// # Errors
283+
///
284+
/// Yields an `Err` if the parameters were found but failed to parse into an
285+
/// instance of type `T`.
286+
///
287+
/// # Panics
288+
///
289+
/// Panic if `key` is not a parameter within the query string.
290+
///
291+
/// # Examples
292+
///
293+
/// ```no_run
294+
/// # use futures::executor::block_on;
295+
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
296+
/// #
297+
/// use tide::Request;
298+
/// use serde::Deserialize;
299+
///
300+
/// #[derive(Deserialize)]
301+
/// struct QueryParams {
302+
/// _param_name: String,
303+
/// }
304+
///
305+
/// let mut app = tide::new();
306+
/// app.at("/").get(|req: Request<()>| async move {
307+
/// let _query_params: QueryParams = req.query::<QueryParams>().unwrap();
308+
/// Ok("")
309+
/// });
310+
/// #
311+
/// # Ok(()) })}
312+
/// ```
270313
pub fn query<'de, T: Deserialize<'de>>(&'de self) -> Result<T, crate::Error> {
271314
// Default to an empty query string if no query parameter has been specified.
272315
// This allows successful deserialisation of structs where all fields are optional

0 commit comments

Comments
 (0)