This documentation regards version 10.1, however the current version is 10.7.0.
Encoding / Decoding
The HTTP spec defines a Content-Encoding header, which signifies whether the entity body of an HTTP message is “encoded” and, if so, by which algorithm. The only commonly used content encodings are compression algorithms.
Currently, Akka HTTP supports the compression and decompression of HTTP requests and responses with the gzip or deflate encodings. The core logic for this lives in the akka.http.scaladsl.coding package.akka.http.javadsl.coding.Coder enum class.
Server side
The support is not enabled automatically, but must be explicitly requested. For enabling message encoding/decoding with Routing DSL see the CodingDirectives.
Client side
There is currently no high-level or automatic support for decoding responses on the client-side.
The following example shows how to decode responses manually based on the Content-Encoding header:
sourceimport akka.actor.ActorSystemimport akka.http.scaladsl.Httpimport akka.http.scaladsl.coding.{Gzip,Deflate,NoCoding}import akka.http.scaladsl.model._, headers.HttpEncodingsimport akka.stream.ActorMaterializerimport scala.concurrent.Futureimplicit val system =ActorSystem()implicit val materializer =ActorMaterializer()import system.dispatcher
val http =Http()
val requests:Seq[HttpRequest]=Seq("https://httpbin.org/gzip",// Content-Encoding: gzip in response"https://httpbin.org/deflate",// Content-Encoding: deflate in response"https://httpbin.org/get"// no Content-Encoding in response).map(uri =>HttpRequest(uri = uri))def decodeResponse(response:HttpResponse):HttpResponse={
val decoder = response.encoding match {caseHttpEncodings.gzip =>GzipcaseHttpEncodings.deflate =>DeflatecaseHttpEncodings.identity =>NoCoding}
decoder.decodeMessage(response)}
val futureResponses:Future[Seq[HttpResponse]]=Future.traverse(requests)(http.singleRequest(_).map(decodeResponse))
futureResponses.futureValue.foreach{ resp =>
system.log.info(s"response is ${resp.toStrict(1.second).futureValue}")}
system.terminate()