1

I'm facing what seems a charset issue of play when decompressing gzip content from rest services. When I try to run the code snippet below, an error is thrown, saying "Malformed JSON. Illegal character ((CTRL-CHAR, code 31))":

val url:String = "https://api.stackexchange.com/2.0/info?site=stackoverflow"
Async {
  WS.url(url)
    .withHeaders("Accept-Encoding" -> "gzip, deflate")
    .get()
    .map { response =>
    Ok("Response: " + (response.json \ "items"))
  }
}

At first I thought it would be a problem in StackExchange API itself, but I tried a similar service, which uses gzip compression as well, and the same error happens. It's hard to fix the code because I don't even know where is the "Illegal character". Is there something missing or it's actually a bug in play?

2 Answers2

0

The clue I can provide is that the first byte of a gzip stream is 31 (0x1f). So you probably need to do something else to cause the gzip stream to be decompressed.

By the way, I recommend that you not accept deflate encoding, just gzip.

Community
  • 1
  • 1
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Ok, got it. I fixed the code as follows: val gzis:GZIPInputStream = new GZIPInputStream(response.ahcResponse.getResponseBodyAsStream); val result:String = Source.fromInputStream(gzis).getLines().mkString("\n") Ok("Response: " + result) –  May 27 '12 at 21:40
0

Here is how it can be done with Play 2.3

// set Http compression: https://www.playframework.com/documentation/2.3.x/ScalaWS
val clientConfig = new DefaultWSClientConfig()
val secureDefaults: AsyncHttpClientConfig = new NingAsyncHttpClientConfigBuilder(clientConfig).build()
val builder = new AsyncHttpClientConfig.Builder(secureDefaults)
builder.setCompressionEnabled(true)
val secureDefaultsWithSpecificOptions: AsyncHttpClientConfig = builder.build()
implicit val implicitClient = new NingWSClient(secureDefaultsWithSpecificOptions)
val response = WS.clientUrl("http://host/endpoint/item").withHeaders(("Accepts-encoding", "gzip")).get()