2017-12-02 20:41:20 +01:00
|
|
|
# HttpRequest & HttpResponse
|
|
|
|
|
|
|
|
## Content encoding
|
|
|
|
|
|
|
|
Actix automatically *compress*/*decompress* payload.
|
|
|
|
Following encodings are supported:
|
|
|
|
|
|
|
|
* Brotli
|
|
|
|
* Gzip
|
|
|
|
* Deflate
|
|
|
|
* Identity
|
|
|
|
|
|
|
|
If request headers contains `Content-Encoding` header, request payload get decompressed
|
|
|
|
according to header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`.
|
|
|
|
|
2017-12-04 03:51:52 +01:00
|
|
|
## JSON Response
|
|
|
|
|
|
|
|
The `Json` type allows you to respond with well-formed JSON data: simply return a value of
|
|
|
|
type Json<T> where T is the type of a structure to serialize into *JSON*. The
|
|
|
|
type `T` must implement the `Serialize` trait from *serde*.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
extern crate actix_web;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
use actix_web::*;
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct MyObj {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
|
|
|
Ok(Json(MyObj{name: req.match_info().query("name")?}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
|
|
|
.resource(r"/a/{name}", |r| r.get(index))
|
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|