Skip to content

Commit f70c8ff

Browse files
authored
docs(example): add a get query method to params example (#2601)
1 parent f51c677 commit f70c8ff

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

examples/params.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
6868
let body = format!("Hello {}, your number is {}", name, number);
6969
Ok(Response::new(body.into()))
7070
}
71+
(&Method::GET, "/get") => {
72+
let query = if let Some(q) = req.uri().query() {
73+
q
74+
} else {
75+
return Ok(Response::builder()
76+
.status(StatusCode::UNPROCESSABLE_ENTITY)
77+
.body(MISSING.into())
78+
.unwrap());
79+
};
80+
let params = form_urlencoded::parse(query.as_bytes())
81+
.into_owned()
82+
.collect::<HashMap<String, String>>();
83+
let page = if let Some(p) = params.get("page") {
84+
p
85+
} else {
86+
return Ok(Response::builder()
87+
.status(StatusCode::UNPROCESSABLE_ENTITY)
88+
.body(MISSING.into())
89+
.unwrap());
90+
};
91+
let body = format!("You requested {}", page);
92+
Ok(Response::new(body.into()))
93+
}
7194
_ => Ok(Response::builder()
7295
.status(StatusCode::NOT_FOUND)
7396
.body(Body::empty())

0 commit comments

Comments
 (0)