warp/filters/query.rs
1//! Query Filters
2
3use futures_util::future;
4use serde::de::DeserializeOwned;
5
6use crate::filter::{filter_fn_one, Filter, One};
7use crate::reject::{self, Rejection};
8
9/// Creates a `Filter` that decodes query parameters to the type `T`.
10///
11/// If cannot decode into a `T`, the request is rejected with a `400 Bad Request`.
12///
13/// # Example
14///
15/// ```
16/// use std::collections::HashMap;
17/// use warp::{
18/// http::Response,
19/// Filter,
20/// };
21///
22/// let route = warp::any()
23/// .and(warp::query::<HashMap<String, String>>())
24/// .map(|map: HashMap<String, String>| {
25/// let mut response: Vec<String> = Vec::new();
26/// for (key, value) in map.into_iter() {
27/// response.push(format!("{}={}", key, value))
28/// }
29/// Response::builder().body(response.join(";"))
30/// });
31/// ```
32///
33/// You can define your custom query object and deserialize with [Serde][Serde]. Ensure to include
34/// the crate in your dependencies before usage.
35///
36/// ```
37/// use serde_derive::{Deserialize, Serialize};
38/// use std::collections::HashMap;
39/// use warp::{
40/// http::Response,
41/// Filter,
42/// };
43///
44/// #[derive(Serialize, Deserialize)]
45/// struct FooQuery {
46/// foo: Option<String>,
47/// bar: u8,
48/// }
49///
50/// let route = warp::any()
51/// .and(warp::query::<FooQuery>())
52/// .map(|q: FooQuery| {
53/// if let Some(foo) = q.foo {
54/// Response::builder().body(format!("foo={}", foo))
55/// } else {
56/// Response::builder().body(format!("bar={}", q.bar))
57/// }
58/// });
59/// ```
60///
61/// For more examples, please take a look at [examples/query_string.rs](https://github.com/seanmonstar/warp/blob/master/examples/query_string.rs).
62///
63/// [Serde]: https://docs.rs/serde
64pub fn query<T: DeserializeOwned + Send + 'static>(
65) -> impl Filter<Extract = One<T>, Error = Rejection> + Copy {
66 filter_fn_one(|route| {
67 let query_string = route.query().unwrap_or_else(|| {
68 tracing::debug!("route was called without a query string, defaulting to empty");
69 ""
70 });
71
72 let query_encoded = serde_urlencoded::from_str(query_string).map_err(|e| {
73 tracing::debug!("failed to decode query string '{}': {:?}", query_string, e);
74 reject::invalid_query()
75 });
76 future::ready(query_encoded)
77 })
78}
79
80/// Creates a `Filter` that returns the raw query string as type String.
81pub fn raw() -> impl Filter<Extract = One<String>, Error = Rejection> + Copy {
82 filter_fn_one(|route| {
83 let route = route
84 .query()
85 .map(|q| q.to_owned())
86 .map(Ok)
87 .unwrap_or_else(|| Err(reject::invalid_query()));
88 future::ready(route)
89 })
90}