1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 11:12:57 +01:00
actix-website/docs/response.md

55 lines
2.4 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Responses
---
import CodeBlock from "@site/src/components/code_block.js";
2018-05-22 23:15:08 +02:00
# Response
2022-02-26 05:41:49 +01:00
A builder-like pattern is used to construct an instance of `HttpResponse`. `HttpResponse` provides several methods that return a `HttpResponseBuilder` instance, which implements various convenience methods for building responses.
2018-05-22 23:15:08 +02:00
> Check the [documentation][responsebuilder] for type descriptions.
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
The methods `.body`, `.finish`, and `.json` finalize response creation and return a constructed _HttpResponse_ instance. If this methods is called on the same builder instance multiple times, the builder will panic.
2018-05-22 23:15:08 +02:00
<CodeBlock example="responses" file="main.rs" section="builder" />
2018-05-22 23:15:08 +02:00
## JSON Response
2022-02-26 05:41:49 +01:00
The `Json` type allows 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_.
For the following example to work, you need to add `serde` to your dependencies in `Cargo.toml`:
```toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }
```
<CodeBlock example="responses" file="json_resp.rs" section="json-resp" />
2022-02-26 05:41:49 +01:00
Using the `Json` type this way instead of calling the `.json` method on a `HttpResponse` makes it immediately clear that the function returns JSON and not any other type of response.
## Content encoding
2018-05-22 23:15:08 +02:00
2022-04-07 16:44:10 +02:00
Actix Web can automatically _compress_ payloads with the [_Compress middleware_][compressmidddleware]. The following codecs are supported:
2018-05-22 23:15:08 +02:00
2022-02-26 05:41:49 +01:00
- Brotli
- Gzip
- Deflate
- Identity
2018-05-22 23:15:08 +02:00
A response's `Content-Encoding` header defaults to `ContentEncoding::Auto`, which performs automatic content compression negotiation based on the request's `Accept-Encoding` header.
2019-06-26 20:15:03 +02:00
<CodeBlock example="responses" file="auto.rs" section="auto" />
2019-06-26 20:15:03 +02:00
Explicitly disable content compression on a handler by setting `Content-Encoding` to an `Identity` value:
2018-05-22 23:15:08 +02:00
<CodeBlock example="responses" file="identity.rs" section="identity" />
2018-05-22 23:15:08 +02:00
When dealing with an already compressed body (for example, when serving pre-compressed assets), set the `Content-Encoding` header on the response manually to bypass the middleware:
<CodeBlock example="responses" file="identity_two.rs" section="identity-two" />
2022-05-08 13:03:52 +02:00
[responsebuilder]: https://docs.rs/actix-web/4/actix_web/struct.HttpResponseBuilder.html
2022-03-06 00:55:35 +01:00
[compressmidddleware]: https://docs.rs/actix-web/4/actix_web/middleware/struct.Compress.html