docs(example): add a get query method to params example (#2601)

This commit is contained in:
0x79756b69
2021-07-22 01:09:38 +09:00
committed by GitHub
parent f51c677dec
commit f70c8ffc7b

View File

@@ -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); let body = format!("Hello {}, your number is {}", name, number);
Ok(Response::new(body.into())) 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() _ => Ok(Response::builder()
.status(StatusCode::NOT_FOUND) .status(StatusCode::NOT_FOUND)
.body(Body::empty()) .body(Body::empty())