1
0
mirror of https://github.com/actix/actix-website synced 2024-11-23 16:31:08 +01:00
actix-website/docs/request.md
Hichem Fantar 2aacdf2f70
Add extension recommendations, fix linting warnings, improve accessibility (#378)
* chore: add VS Code extension recommendations

* Update image URLs in README and documentation files

* chore: disable no-inline-html rule

* chore: use standard md/mdx syntax, and use .jsx for react components

* chore: fix email links in Code of Conduct

The commit message suggests fixing the email links in the Code of Conduct file to use the correct `mailto:` syntax.

* chore: update actix-web error helper links

Update the links to the `actix-web` error helper traits in the `databases.md` and `errors.md` files to use the correct URLs.

* chore: restore unused actix-web error helper links

* Update src/pages/community/coc.md

Co-authored-by: Rob Ede <robjtede@icloud.com>

* Update docs/getting-started.md

Co-authored-by: Rob Ede <robjtede@icloud.com>

---------

Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-05-27 20:55:31 +00:00

3.6 KiB

title
Requests

import CodeBlock from "@site/src/components/code_block";

JSON Request

There are several options for json body deserialization.

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.

First example of json of JSON Request depends on serde:

[dependencies]
serde = { version = "1.0", features = ["derive"] }

Second example of JSON Request depends on serde and serde_json and futures:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
futures = "0.3"

If you want to add default value for a field, refer to serde's documentation.

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.

A complete example for both options is available in examples directory.

Content Encoding

Actix Web automatically decompresses payloads. The following codecs are supported:

  • Brotli
  • Gzip
  • Deflate
  • Zstd

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.

Chunked transfer encoding

Actix automatically decodes chunked encoding. The web::Payload 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.

Multipart body

Actix Web provides multipart stream support with an external crate, actix-multipart.

A full example is available in the examples directory.

Urlencoded body

Actix Web provides support for application/x-www-form-urlencoded encoded bodies with the web::Form extractor 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.

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: