compressResponseIfRequested
Only compresses the response when specifically requested by the Accept-Encoding request header (i.e. the default is “no compression”).
Signature
def compressResponseIfRequested()(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 compressResponseIfRequested directive is an alias for compressResponse(NoEncoding, Gzip, Deflate) and will 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 | uncompressed |
For an overview of the different compressResponse directives see When to use which compression directive?.
Example
val route = compressResponseIfRequested() { complete("content") }
Get("/") ~> route ~> check {
response must haveContentEncoding(identity)
}
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 {
response must haveContentEncoding(identity)
}