1
0
mirror of https://github.com/actix/actix-website synced 2024-11-27 18:12:57 +01:00
actix-website/content/docs/request.md

92 lines
3.1 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Requests
menu: docs_advanced
weight: 200
---
# Content Encoding
Actix automatically *decompresses* payloads. The following codecs are supported:
* Brotli
2019-06-17 20:34:23 +02:00
* Chunked
* Compress
2018-05-22 23:15:08 +02:00
* Gzip
* Deflate
* Identity
2019-06-17 20:34:23 +02:00
* Trailers
* EncodingExt
2018-05-22 23:15:08 +02:00
If request headers contain a `Content-Encoding` header, the request payload is decompressed
according to the header value. Multiple codecs are not supported,
i.e: `Content-Encoding: br, gzip`.
# JSON Request
There are several options for json body deserialization.
The first option is to use *Json* extractor. First, you define a handler function
2019-06-17 20:34:23 +02:00
that accepts `Json<T>` as a parameter, then, you use the `.to()` method for registering
2018-05-22 23:15:08 +02:00
this handler. It is also possible to accept arbitrary valid json object by
using `serde_json::Value` as a type `T`.
2019-06-17 20:34:23 +02:00
{{< include-example example="requests" file="main.rs" section="json-request" >}}
2018-05-22 23:15:08 +02:00
You may also manually load the payload into memory and then deserialize it.
In the following example, we will deserialize a *MyObj* struct. We need to load the request
body first and then deserialize the json into an object.
2019-06-17 20:34:23 +02:00
{{< include-example example="requests" file="manual.rs" section="json-manual" >}}
2018-05-22 23:15:08 +02:00
> A complete example for both options is available in
> [examples directory](https://github.com/actix/examples/tree/master/json/).
# Chunked transfer encoding
Actix automatically decodes *chunked* encoding. `HttpRequest::payload()` already contains
the decoded byte stream. If the request payload is compressed with one of the supported
compression codecs (br, gzip, deflate), then the byte stream is decompressed.
# Multipart body
Actix provides multipart stream support.
[*Multipart*](../../actix-web/actix_web/multipart/struct.Multipart.html) is implemented as
a stream of multipart items. Each item can be a
[*Field*](../../actix-web/actix_web/multipart/struct.Field.html) or a nested
*Multipart* stream.`HttpResponse::multipart()` returns the *Multipart* stream
for the current request.
The following demonstrates multipart stream handling for a simple form:
2019-06-17 20:34:23 +02:00
{{< include-example example="requests" file="multipart.rs" section="multipart" >}}
2018-05-22 23:15:08 +02:00
> A full example is available in the
> [examples directory](https://github.com/actix/examples/tree/master/multipart/).
# Urlencoded body
Actix provides support for *application/x-www-form-urlencoded* encoded bodies.
`HttpResponse::urlencoded()` returns a
[*UrlEncoded*](../../actix-web/actix_web/dev/struct.UrlEncoded.html) future, which resolves
to the deserialized instance. The type of the instance must implement the
`Deserialize` trait from *serde*.
The *UrlEncoded* future can resolve into an error in several cases:
* content type is not `application/x-www-form-urlencoded`
* transfer encoding is `chunked`.
* content-length is greater than 256k
* payload terminates with error.
2019-06-17 20:34:23 +02:00
{{< include-example example="requests" file="urlencoded.rs" section="urlencoded" >}}
2018-05-22 23:15:08 +02:00
# Streaming request
*HttpRequest* is a stream of `Bytes` objects. It can be used to read the request
body payload.
In the following example, we read and print the request payload chunk by chunk:
2019-06-17 20:34:23 +02:00
{{< include-example example="requests" file="streaming.rs" section="streaming" >}}