mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
cleanup example
This commit is contained in:
parent
c36ad06332
commit
df2aa42dad
@ -9,7 +9,7 @@ extern crate serde_json;
|
|||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use futures::future::{Future, ok, err, result};
|
use futures::future::{Future, ok, err};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct MyObj {
|
struct MyObj {
|
||||||
@ -18,25 +18,23 @@ struct MyObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn index(mut req: HttpRequest) -> Result<Box<Future<Item=HttpResponse, Error=Error>>> {
|
fn index(mut req: HttpRequest) -> Result<Box<Future<Item=HttpResponse, Error=Error>>> {
|
||||||
// check content-type, early error exit
|
// check content-type
|
||||||
if req.content_type() != "application/json" {
|
if req.content_type() != "application/json" {
|
||||||
return Err(error::ErrorBadRequest("wrong content-type").into())
|
return Err(error::ErrorBadRequest("wrong content-type").into())
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Box::new(
|
Ok(Box::new(
|
||||||
// load request body
|
req.payload_mut() // <- load request body
|
||||||
req.payload_mut()
|
|
||||||
.readany()
|
.readany()
|
||||||
.fold(BytesMut::new(), |mut body, chunk| {
|
.fold(BytesMut::new(), |mut body, chunk| {
|
||||||
body.extend(chunk);
|
body.extend(chunk);
|
||||||
result::<_, error::PayloadError>(Ok(body))
|
ok::<_, error::PayloadError>(body)
|
||||||
})
|
})
|
||||||
.map_err(|e| Error::from(e))
|
.map_err(|e| Error::from(e))
|
||||||
.and_then(|body| {
|
.and_then(|body| { // <- body is loaded, now we can deserialize json
|
||||||
// body is loaded, now we can deserialize json
|
|
||||||
match serde_json::from_slice::<MyObj>(&body) {
|
match serde_json::from_slice::<MyObj>(&body) {
|
||||||
Ok(obj) => {
|
Ok(obj) => {
|
||||||
println!("MODEL: {:?}", obj); // <- do something with payload
|
println!("model: {:?}", obj); // <- do something with payload
|
||||||
ok(httpcodes::HTTPOk.response()) // <- send response
|
ok(httpcodes::HTTPOk.response()) // <- send response
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -59,8 +59,8 @@ fn index(req: HttpRequest) -> HttpResponse {
|
|||||||
|
|
||||||
## JSON Request
|
## JSON Request
|
||||||
|
|
||||||
Unfortunately, because of async nature of actix web framework, deserializing json
|
Unfortunately, because of async nature of actix web framework, json requests deserialization
|
||||||
requests is not very ergonomic process. First you need to load whole body into
|
is not very ergonomic process. First you need to load whole body into a
|
||||||
temporal storage and only then you can deserialize it.
|
temporal storage and only then you can deserialize it.
|
||||||
|
|
||||||
Here is simple example. We will deserialize *MyObj* struct.
|
Here is simple example. We will deserialize *MyObj* struct.
|
||||||
@ -73,15 +73,14 @@ struct MyObj {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
We need to load request body first.
|
We need to load request body first and then deserialize json into object.
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
fn index(mut req: HttpRequest) -> Future<Item=HttpResponse, Error=Error> {
|
fn index(mut req: HttpRequest) -> Future<Item=HttpResponse, Error=Error> {
|
||||||
|
|
||||||
req.payload_mut().readany()
|
req.payload_mut().readany()
|
||||||
.fold(BytesMut::new(), |mut body, chunk| { // <- load request body
|
.fold(BytesMut::new(), |mut body, chunk| { // <- load request body
|
||||||
body.extend(chunk);
|
body.extend(chunk);
|
||||||
result::<_, error::PayloadError>(Ok(body))
|
ok(body)
|
||||||
})
|
})
|
||||||
.and_then(|body| { // <- body is loaded, now we can deserialize json
|
.and_then(|body| { // <- body is loaded, now we can deserialize json
|
||||||
let obj = serde_json::from_slice::<MyObj>(&body).unwrap();
|
let obj = serde_json::from_slice::<MyObj>(&body).unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user