compressResponse
Uses the first of a given number of encodings that the client accepts. If none are accepted the request
is rejected with an UnacceptedResponseEncodingRejection
.
Signature
def compressResponse()(implicit refFactory: ActorRefFactory): Directive0
def compressResponse(e1: Encoder)(implicit refFactory: ActorRefFactory): Directive0
def compressResponse(e1: Encoder, e2: Encoder)(implicit refFactory: ActorRefFactory): Directive0
def compressResponse(e1: Encoder, e2: Encoder, e3: Encoder)(implicit refFactory: ActorRefFactory): Directive0
The signature shown is simplified, the real signature uses magnets. [1]
[1] | See The Magnet Pattern for an explanation of magnet-based overloading. |
Description
The compressResponse
directive allows to specify zero to three encoders to try in the specified order.
If none are specified the tried list is Gzip
, Deflate
, and then NoEncoding
.
The compressResponse()
directive (without an explicit list of encoders given) will therefore behave as follows:
Accept-Encoding header |
resulting response |
---|---|
Accept-Encoding: gzip |
compressed with Gzip |
Accept-Encoding: deflate |
compressed with Deflate |
Accept-Encoding: deflate, gzip |
compressed with Gzip |
Accept-Encoding: identity |
uncompressed |
no Accept-Encoding header present |
compressed with Gzip |
For an overview of the different compressResponse
directives see When to use which compression directive?.
Example
This example shows the behavior of compressResponse
without any encoders specified:
val route = compressResponse() { complete("content") }
Get("/") ~> route ~> check {
response must haveContentEncoding(gzip)
}
Get("/") ~> `Accept-Encoding`(gzip, deflate) ~> route ~> check {
response must haveContentEncoding(gzip)
}
Get("/") ~> `Accept-Encoding`(deflate) ~> route ~> check {
response must haveContentEncoding(deflate)
}
Get("/") ~> `Accept-Encoding`(identity) ~> route ~> check {
status === StatusCodes.OK
response must haveContentEncoding(identity)
responseAs[String] === "content"
}
This example shows the behaviour of compressResponse(Gzip)
:
val route = compressResponse(Gzip) { complete("content") }
Get("/") ~> route ~> check {
response must haveContentEncoding(gzip)
}
Get("/") ~> `Accept-Encoding`(gzip, deflate) ~> route ~> check {
response must haveContentEncoding(gzip)
}
Get("/") ~> `Accept-Encoding`(deflate) ~> route ~> check {
rejection === UnacceptedResponseEncodingRejection(gzip)
}
Get("/") ~> `Accept-Encoding`(identity) ~> route ~> check {
rejection === UnacceptedResponseEncodingRejection(gzip)
}