Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add an example for get method. #2601

Merged
merged 3 commits into from Jul 21, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/params.rs
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{
seanmonstar marked this conversation as resolved.
Show resolved Hide resolved
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