2018-05-22 23:15:08 +02:00
|
|
|
---
|
|
|
|
title: Responses
|
|
|
|
menu: docs_advanced
|
|
|
|
weight: 210
|
|
|
|
---
|
|
|
|
|
|
|
|
# Response
|
|
|
|
|
2019-06-26 07:58:47 +02: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
|
|
|
|
2019-06-25 05:36:32 +02:00
|
|
|
> Check the [documentation][responsebuilder] for type descriptions.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-26 07:58:47 +02: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
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
{{< include-example example="responses" file="main.rs" section="builder" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
# Content encoding
|
|
|
|
|
2020-09-19 15:49:34 +02:00
|
|
|
Actix-web can automatically *compress* payloads with the [*Compress middleware*][compressmidddleware].
|
2019-06-26 20:15:03 +02:00
|
|
|
The following codecs are supported:
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
* Brotli
|
|
|
|
* Gzip
|
|
|
|
* Deflate
|
|
|
|
* Identity
|
|
|
|
|
2019-06-26 20:15:03 +02:00
|
|
|
{{< include-example example="responses" file="compress.rs" section="compress" >}}
|
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
Response payload is compressed based on the *encoding* parameter from the
|
2019-06-26 20:15:03 +02:00
|
|
|
`middleware::BodyEncoding` trait. By default, `ContentEncoding::Auto` is used. If
|
|
|
|
`ContentEncoding::Auto` is selected, then the compression depends on the request's
|
|
|
|
`Accept-Encoding` header.
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
> `ContentEncoding::Identity` can be used to disable compression.
|
|
|
|
> If another content encoding is selected, the compression is enforced for that codec.
|
|
|
|
|
2019-06-26 20:15:03 +02:00
|
|
|
For example, to enable `brotli` for a single handler use `ContentEncoding::Br`:
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
{{< include-example example="responses" file="brotli.rs" section="brotli" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-26 20:15:03 +02:00
|
|
|
or for the entire application:
|
|
|
|
|
|
|
|
{{< include-example example="responses" file="brotli_two.rs" section="brotli-two" >}}
|
|
|
|
|
2019-06-26 07:58:47 +02:00
|
|
|
In this case we explicitly disable content compression by setting content encoding to
|
|
|
|
an `Identity` value:
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
{{< include-example example="responses" file="identity.rs" section="identity" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2018-12-11 22:59:44 +01:00
|
|
|
When dealing with an already compressed body (for example when serving assets),
|
2019-06-26 20:15:03 +02:00
|
|
|
set the content encoding to `Identity` to avoid compressing the already compressed
|
2018-12-11 22:59:44 +01:00
|
|
|
data and set the `content-encoding` header manually:
|
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
{{< include-example example="responses" file="identity_two.rs" section="identity-two" >}}
|
2018-12-11 22:59:44 +01:00
|
|
|
|
2018-05-22 23:15:08 +02:00
|
|
|
Also it is possible to set default content encoding on application level, by
|
|
|
|
default `ContentEncoding::Auto` is used, which implies automatic content compression
|
|
|
|
negotiation.
|
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
{{< include-example example="responses" file="auto.rs" section="auto" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
|
|
|
# JSON Response
|
|
|
|
|
|
|
|
The `Json` type allows to respond with well-formed JSON data: simply return a value of
|
2020-01-02 13:33:33 +01:00
|
|
|
type `Json<T>` where `T` is the type of a structure to serialize into *JSON*.
|
2018-05-22 23:15:08 +02:00
|
|
|
The type `T` must implement the `Serialize` trait from *serde*.
|
|
|
|
|
2019-06-17 21:15:33 +02:00
|
|
|
{{< include-example example="responses" file="json_resp.rs" section="json-resp" >}}
|
2018-05-22 23:15:08 +02:00
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
[responsebuilder]: https://docs.rs/actix-web/3/actix_web/dev/struct.HttpResponseBuilder.html
|
|
|
|
[compressmidddleware]: https://docs.rs/actix-web/3/actix_web/middleware/struct.Compress.html
|