1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

update urlencoded example in guide

This commit is contained in:
Nikolay Kim 2018-04-02 15:08:49 -07:00
parent a6cbdde43f
commit ef6f310060
2 changed files with 12 additions and 7 deletions

View File

@ -256,8 +256,9 @@ A full example is available in the
Actix provides support for *application/x-www-form-urlencoded* encoded bodies. Actix provides support for *application/x-www-form-urlencoded* encoded bodies.
`HttpResponse::urlencoded()` returns a `HttpResponse::urlencoded()` returns a
[*UrlEncoded*](../actix_web/dev/struct.UrlEncoded.html) future, which resolves [*UrlEncoded*](../actix_web/dev/struct.UrlEncoded.html) future, which resolves
into `HashMap<String, String>` which contains decoded parameters. to the deserialized instance, the type of the instance must implement the
The *UrlEncoded* future can resolve into a error in several cases: `Deserialize` trait from *serde*. The *UrlEncoded* future can resolve into
a error in several cases:
* content type is not `application/x-www-form-urlencoded` * content type is not `application/x-www-form-urlencoded`
* transfer encoding is `chunked`. * transfer encoding is `chunked`.
@ -268,14 +269,20 @@ The *UrlEncoded* future can resolve into a error in several cases:
```rust ```rust
# extern crate actix_web; # extern crate actix_web;
# extern crate futures; # extern crate futures;
#[macro_use] extern crate serde_derive;
use actix_web::*; use actix_web::*;
use futures::future::{Future, ok}; use futures::future::{Future, ok};
#[derive(Deserialize)]
struct FormData {
username: String,
}
fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
req.urlencoded() // <- get UrlEncoded future req.urlencoded::<FormData>() // <- get UrlEncoded future
.from_err() .from_err()
.and_then(|params| { // <- url encoded parameters .and_then(|data| { // <- deserialized instance
println!("==== BODY ==== {:?}", params); println!("USERNAME: {:?}", data.username);
ok(HttpResponse::Ok().into()) ok(HttpResponse::Ok().into())
}) })
.responder() .responder()
@ -283,7 +290,6 @@ fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
# fn main() {} # fn main() {}
``` ```
## Streaming request ## Streaming request
*HttpRequest* is a stream of `Bytes` objects. It can be used to read the request *HttpRequest* is a stream of `Bytes` objects. It can be used to read the request

View File

@ -287,7 +287,6 @@ mod tests {
} }
} }
#[derive(Deserialize)] #[derive(Deserialize)]
struct MyStruct { struct MyStruct {
key: String, key: String,