docs(example): add a get query method to params example (#2601)
This commit is contained in:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user