2018-05-22 23:15:08 +02:00
---
title: Requests
menu: docs_advanced
weight: 200
---
# JSON Request
There are several options for json body deserialization.
2022-02-26 05:41:49 +01:00
The first option is to use _Json_ extractor. First, you define a handler function that accepts `Json<T>` as a parameter, then, you use the `.to()` method for registering this handler. It is also possible to accept arbitrary valid json object by using `serde_json::Value` as a type `T` .
2018-05-22 23:15:08 +02:00
2021-02-28 02:42:22 +01:00
First example of json of `JSON Request` depends on `serde` :
2022-02-26 05:41:49 +01:00
2021-02-28 02:42:22 +01:00
```toml
[dependencies]
2022-03-14 14:44:17 +01:00
serde = { version = "1.0", features = ["derive"] }
2022-02-26 05:41:49 +01:00
```
2022-03-14 15:26:01 +01:00
Second example of `JSON Request` depends on `serde` and `serde_json` and `futures` :
2022-02-26 05:41:49 +01:00
2021-02-28 02:42:22 +01:00
```toml
[dependencies]
2022-03-14 15:26:01 +01:00
serde = { version = "1.0", features = ["derive"] }
2021-02-28 02:42:22 +01:00
serde_json = "1"
2022-03-14 15:26:01 +01:00
futures = "0.3"
2021-02-28 02:42:22 +01:00
```
2022-02-26 05:41:49 +01:00
2021-02-28 02:42:22 +01:00
If you want to add default value for a field, refer to `serde` 's [documentation ](https://serde.rs/attr-default.html ).
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.
2022-02-26 05:41:49 +01:00
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.
2018-05-22 23:15:08 +02:00
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
2019-06-25 05:36:32 +02:00
> A complete example for both options is available in [examples directory][examples].
2018-05-22 23:15:08 +02:00
2021-12-05 15:41:37 +01:00
# Content Encoding
2022-02-26 05:41:49 +01:00
Actix-web automatically _decompresses_ payloads. The following codecs are supported:
2021-12-05 15:41:37 +01:00
2022-02-26 05:41:49 +01:00
- Brotli
- Gzip
- Deflate
- Zstd
2021-12-05 15:41:37 +01:00
2022-02-26 05:41:49 +01: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` .
2021-12-05 15:41:37 +01:00
2018-05-22 23:15:08 +02:00
# Chunked transfer encoding
2022-02-26 05:41:49 +01:00
Actix automatically decodes _chunked_ encoding. The [`web::Payload`][payloadextractor] extractor 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.
2018-05-22 23:15:08 +02:00
# Multipart body
2019-06-28 19:31:30 +02:00
Actix-web provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
2018-05-22 23:15:08 +02:00
2019-06-25 05:36:32 +02:00
> A full example is available in the [examples directory][multipartexample].
2018-05-22 23:15:08 +02:00
# Urlencoded body
2022-02-26 05:41:49 +01:00
Actix-web provides support for _application/x-www-form-urlencoded_ encoded bodies with the [`web::Form`][formencoded] extractor which resolves to the deserialized instance. The type of the instance must implement the `Deserialize` trait from _serde_ .
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
The _UrlEncoded_ future can resolve into an error in several cases:
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
- content type is not `application/x-www-form-urlencoded`
- transfer encoding is `chunked` .
- content-length is greater than 256k
- payload terminates with error.
2018-05-22 23:15:08 +02:00
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
2022-02-26 05:41:49 +01:00
_HttpRequest_ is a stream of `Bytes` objects. It can be used to read the request body payload.
2018-05-22 23:15:08 +02:00
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" > }}
2019-06-25 05:36:32 +02:00
2021-02-26 19:48:27 +01:00
[examples]: https://github.com/actix/examples/tree/master/json/json
2019-12-28 18:32:47 +01:00
[multipartstruct]: https://docs.rs/actix-multipart/0.2/actix_multipart/struct.Multipart.html
[fieldstruct]: https://docs.rs/actix-multipart/0.2/actix_multipart/struct.Field.html
2021-02-26 19:48:27 +01:00
[multipartexample]: https://github.com/actix/examples/tree/master/forms/multipart
2022-03-06 00:55:35 +01:00
[urlencoded]: https://docs.rs/actix-web/4/actix_web/dev/struct.UrlEncoded.html
[payloadextractor]: https://docs.rs/actix-web/4/actix_web/web/struct.Payload.html
2019-06-26 07:27:17 +02:00
[multipartcrate]: https://crates.io/crates/actix-multipart
2022-03-06 00:55:35 +01:00
[formencoded]: https://docs.rs/actix-web/4/actix_web/web/struct.Form.html