1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

simplify json example

This commit is contained in:
Nikolay Kim
2017-12-20 16:05:07 -08:00
parent df2aa42dad
commit 50891986bc
2 changed files with 27 additions and 24 deletions

View File

@ -77,16 +77,20 @@ We need to load request body first and then deserialize json into object.
```rust,ignore
fn index(mut req: HttpRequest) -> Future<Item=HttpResponse, Error=Error> {
req.payload_mut().readany()
.fold(BytesMut::new(), |mut body, chunk| { // <- load request body
body.extend(chunk);
ok(body)
})
.and_then(|body| { // <- body is loaded, now we can deserialize json
let obj = serde_json::from_slice::<MyObj>(&body).unwrap();
println!("MODEL: {:?}", obj); // <- do something with payload
ok(httpcodes::HTTPOk.response()) // <- send response
})
// `concat2` will asynchronously read each chunk of the request body and
// return a single, concatenated, chunk
req.payload_mut().readany().concat2()
// `Future::from_err` acts like `?` in that it coerces the error type from
// the future into the final error type
.from_err()
// `Future::and_then` can be used to merge an asynchronous workflow with a
// synchronous workflow
.and_then(|body| { // <- body is loaded, now we can deserialize json
let obj = serde_json::from_slice::<MyObj>(&body).unwrap();
ok(httpcodes::HTTPOk.build() // <- send response
.content_type("application/json")
.json(obj).unwrap())
})
}
```