1
0
mirror of https://github.com/actix/actix-website synced 2025-07-01 01:04:27 +02:00

extractors: done-ish.

This commit is contained in:
Cameron Dershem
2019-06-20 04:20:12 -04:00
parent 59f010461a
commit c4c32091a6
13 changed files with 199 additions and 142 deletions

View File

@ -8,6 +8,7 @@ pub mod json_one;
pub mod json_two;
pub mod multiple;
pub mod path_one;
pub mod path_three;
pub mod path_two;
pub mod query;
@ -17,13 +18,13 @@ struct MyInfo {
id: u32,
}
// <main>
// Option 1: passed as a parameter to a handler function
// <option-one>
fn index(path: web::Path<(String, String)>, json: web::Json<MyInfo>) -> impl Responder {
format!("{} {} {} {}", path.0, path.1, json.id, json.username)
}
// </option-one>
// Option 2: accessed by calling extract() on the Extractor
// <option-two>
fn extract(req: HttpRequest) -> impl Responder {
let params = web::Path::<(String, String)>::extract(&req).unwrap();
@ -33,7 +34,7 @@ fn extract(req: HttpRequest) -> impl Responder {
format!("{} {} {} {}", params.0, params.1, info.username, info.id)
}
// </main>
// </option-two>
fn main() {
HttpServer::new(|| {