Skip to content

Commit

Permalink
docs(example): add a get query method to params example (#2601)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x79756b69 committed Jul 21, 2021
1 parent f51c677 commit f70c8ff
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ async fn param_example(req: Request<Body>) -> Result<Response<Body>, hyper::Erro
let body = format!("Hello {}, your number is {}", name, number);
Ok(Response::new(body.into()))
}
(&Method::GET, "/get") => {
let query = if let Some(q) = req.uri().query() {
q
} else {
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(MISSING.into())
.unwrap());
};
let params = form_urlencoded::parse(query.as_bytes())
.into_owned()
.collect::<HashMap<String, String>>();
let page = if let Some(p) = params.get("page") {
p
} else {
return Ok(Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(MISSING.into())
.unwrap());
};
let body = format!("You requested {}", page);
Ok(Response::new(body.into()))
}
_ => Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::empty())
Expand Down

0 comments on commit f70c8ff

Please sign in to comment.