decompressRequest

Decompresses the request if it is can be decoded with one of the given decoders. Otherwise, the request is rejected with an UnsupportedRequestEncodingRejection(supportedEncoding).

Signature

def decompressRequest(): Directive0 
def decompressRequest(first: Decoder, more: Decoder*): Directive0 

Description

The decompressRequest directive allows either to specify a list of decoders or none at all. If no Decoder is specified Gzip, Deflate, or NoEncoding will be tried.

The decompressRequest directive will behave as follows:

Content-Encoding header resulting request
Content-Encoding: gzip decompressed
Content-Encoding: deflate decompressed
Content-Encoding: identity unchanged
no Content-Encoding header present unchanged

For an overview of the different decompressRequest directives and which one to use when, see When to use which decompression directive?.

Example

This example shows the behavior of decompressRequest() without any decoders specified:

val route =
  decompressRequest() {
    entity(as[String]) { content: String =>
      complete(s"Request content: '$content'")
    }
  }

Get("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
  responseAs[String] === "Request content: 'Hello'"
}
Get("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
  responseAs[String] === "Request content: 'Hello'"
}
Get("/", "hello uncompressed") ~> `Content-Encoding`(identity) ~> route ~> check {
  responseAs[String] === "Request content: 'hello uncompressed'"
}

This example shows the behaviour of decompressRequest(Gzip, NoEncoding):

val route =
  decompressRequest(Gzip, NoEncoding) {
    entity(as[String]) { content: String =>
      complete(s"Request content: '$content'")
    }
  }

Get("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
  responseAs[String] === "Request content: 'Hello'"
}
Get("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
  rejections === List(UnsupportedRequestEncodingRejection(gzip), UnsupportedRequestEncodingRejection(identity))
}
Get("/", "hello uncompressed") ~> `Content-Encoding`(identity) ~> route ~> check {
  responseAs[String] === "Request content: 'hello uncompressed'"
}