From f70c8ffc7b949a0c9135bfc95caf676e4dedcd4e Mon Sep 17 00:00:00 2001 From: 0x79756b69 <76654022+0x79756b69@users.noreply.github.com> Date: Thu, 22 Jul 2021 01:09:38 +0900 Subject: [PATCH] docs(example): add a get query method to params example (#2601) --- examples/params.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/examples/params.rs b/examples/params.rs index 38c9bfabad..87c2368928 100644 --- a/examples/params.rs +++ b/examples/params.rs @@ -68,6 +68,29 @@ async fn param_example(req: Request) -> Result, 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::>(); + 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())