English 中文(简体)
播放框架2、休息服务和gzip解压缩
原标题:Play Framework 2, Rest Services and gzip decompression

在从rest服务解压缩gzip内容时,我面临着一个字符集问题。当我尝试运行下面的代码片段时,会抛出一个错误,说“JSON格式错误。非法字符((CTRL-CHAR,代码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"))
  }
}

起初,我认为StackExchange API本身会有问题,但我尝试了一个类似的服务,它也使用gzip压缩,同样的错误也发生了。很难修复代码,因为我甚至不知道“非法字符”在哪里。是缺少了什么,还是实际上是一个bug在起作用?

问题回答

我可以提供的线索是gzip流的第一个字节是31(0x1f)。因此,您可能需要采取其他措施来解压缩gzip流。

顺便说一句,我建议你不接受deflate编码,只接受gzip。

以下是如何使用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()




相关问题
How to Decompress nested GZip (TGZ) files in C#

I am receiving a TGZ file that will contain one plain text file along with possibly one or more nested TGZ files. I have figured out how to decompress the main TGZ file and read the plain text file ...

Compress data before storage on Google App Engine

I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I ...

How to minify Javascript code

JQuery has two versions for download, one is Production (19KB, Minified and Gzipped), and the other is Development (120KB, Uncompressed Code). Now the compact 19kb version, if you download it, you ...

Unzipping part of a .gz file using python

So here s the problem. I have sample.gz file which is roughly 60KB in size. I want to decompress the first 2000 bytes of this file. I am running into CRC check failed error, I guess because the gzip ...

Generate and serving gz compressed in ASP.NET

Hi I need to serve from a ASHX a GZ compressed file. In the code I already have the string in clear: public void ProcessRequest(HttpContext context) { // this is the code without compression ...

Unzip part of a file using python gzip module

I am trying to unzip a gzipped file in Python using the gzip module. The pre-condition is that, I get 160 bytesof data at a time, and I need to unzip it before I request for the next 160 bytes. ...

How to process compressed data in Java

I have some data which takes up more than 50MB in an uncompressed file, but compresses down to less than half a MB using gzip. Most of this is numerical data. I m trying to figure out how to process ...

Internet Explorer only part loading JavaScript/CSS

I m having trouble with my local development environment where IE (6 through to 8) is only part loading JavaScript/CSS files. It throws random errors at random places in jquery.min.js every time I ...

热门标签