1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-17 05:36:36 +00: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

@ -1,17 +1,15 @@
extern crate actix;
extern crate actix_web;
extern crate bytes;
extern crate futures;
extern crate env_logger;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
use actix_web::*;
use bytes::BytesMut;
use futures::Stream;
use futures::future::{Future, ok, err};
#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
struct MyObj {
name: String,
number: i32,
@ -24,22 +22,23 @@ fn index(mut req: HttpRequest) -> Result<Box<Future<Item=HttpResponse, Error=Err
}
Ok(Box::new(
req.payload_mut() // <- load request body
.readany()
.fold(BytesMut::new(), |mut body, chunk| {
body.extend(chunk);
ok::<_, error::PayloadError>(body)
})
.map_err(|e| Error::from(e))
// `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
match serde_json::from_slice::<MyObj>(&body) {
Ok(obj) => {
println!("model: {:?}", obj); // <- do something with payload
ok(httpcodes::HTTPOk.response()) // <- send response
ok(httpcodes::HTTPOk.build() // <- send response
.content_type("application/json")
.json(obj).unwrap())
},
Err(e) => {
err(error::ErrorBadRequest(e).into())
}
Err(e) => err(error::ErrorBadRequest(e).into())
}
})))
}

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())
})
}
```