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

update json example and guide info

This commit is contained in:
Nikolay Kim
2017-12-25 08:12:13 -08:00
parent b0c8fa03f0
commit a578262f73
3 changed files with 57 additions and 9 deletions

View File

@ -13,9 +13,9 @@ a response object. More informatin is available in [handler section](../qs_4.htm
Resource configuraiton is the act of adding a new resource to an application.
A resource has a name, which acts as an identifier to be used for URL generation.
The name also allows developers to add routes to existing resources.
A resource also has a pattern, meant to match against the *PATH* portion of a *URL*
(the portion following the scheme and port, e.g., */foo/bar* in the
*URL* *http://localhost:8080/foo/bar*).
A resource also has a pattern, meant to match against the *PATH* portion of a *URL*,
it does not match against *QUERY* portion (the portion following the scheme and
port, e.g., */foo/bar* in the *URL* *http://localhost:8080/foo/bar?q=value*).
The [Application::resource](../actix_web/struct.Application.html#method.resource) methods
add a single resource to application routing table. This method accepts *path pattern*

View File

@ -69,7 +69,6 @@ deserialized value.
# extern crate actix;
# extern crate actix_web;
# extern crate futures;
# extern crate env_logger;
# extern crate serde_json;
# #[macro_use] extern crate serde_derive;
# use actix_web::*;
@ -95,8 +94,18 @@ Or you can manually load payload into memory and ther deserialize it.
Here is simple example. We will deserialize *MyObj* struct. 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> {
```rust
# extern crate actix_web;
# extern crate futures;
# use actix_web::*;
# #[macro_use] extern crate serde_derive;
extern crate serde_json;
use futures::{Future, Stream};
#[derive(Serialize, Deserialize)]
struct MyObj {name: String, number: i32}
fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
// `concat2` will asynchronously read each chunk of the request body and
// return a single, concatenated, chunk
req.payload_mut().readany().concat2()
@ -109,10 +118,12 @@ fn index(mut req: HttpRequest) -> Future<Item=HttpResponse, Error=Error> {
let obj = serde_json::from_slice::<MyObj>(&body)?;
Ok(httpcodes::HTTPOk.build().json(obj)?) // <- send response
})
.responder()
}
# fn main() {}
```
Example is available in
Complete example for both options is available in
[examples directory](https://github.com/actix/actix-web/tree/master/examples/json/).