Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect header check with Content-Encoding deflate #71

Open
Jasper-M opened this issue Oct 26, 2020 · 0 comments
Open

Incorrect header check with Content-Encoding deflate #71

Jasper-M opened this issue Oct 26, 2020 · 0 comments

Comments

@Jasper-M
Copy link

Jasper-M commented Oct 26, 2020

I am calling a REST API which I cannot disclose, but it returns a response with Content-Encoding: deflate.

scala> requests.get(url = redactedUrl, params = redactedParams)
java.util.zip.ZipException: incorrect header check
  at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
  at java.io.FilterInputStream.read(FilterInputStream.java:107)
  at geny.Internal$.transfer0(Internal.scala:40)
  at geny.Internal$.transfer(Internal.scala:61)
  at geny.Readable.$anonfun$writeBytesTo$2(Writable.scala:64)
  at geny.Readable.$anonfun$writeBytesTo$2$adapted(Writable.scala:64)
  at requests.Requester$$anon$1.processWrappedStream$1(Requester.scala:338)
  at requests.Requester$$anon$1.readBytesThrough(Requester.scala:345)
  at geny.Readable.writeBytesTo(Writable.scala:64)
  at geny.Readable.writeBytesTo$(Writable.scala:64)
  at requests.Requester$$anon$1.writeBytesTo(Requester.scala:165)
  at requests.Requester.apply(Requester.scala:114)
  ... 40 elided

The issue here essentially boils down to this stackoverflow post. The real bug is probably with the REST API that I'm calling, but it should be said that regular browsers, curl, wget and Python Requests can handle this discrepancy. The culprit is most likely some misnamed PHP function.

A workaround is to manually handle decompression. Something akin to this:

def inflateResponse(r: geny.Readable): String = {
  val inflater = new Inflater(true)
  val out = new ByteArrayOutputStream()
  val inflaterOut = new InflaterOutputStream(out, inflater)
  try {
    r.writeBytesTo(inflaterOut)
    out.toString("UTF-8")
  } 
  finally {
    inflaterOut.close()
  }
}

val resp = requests.get(url = redactedUrl, params = redactedParams, autoDecompress = false)
inflateResponse(resp)
// or preferably
val str = requests.get.stream(url = redactedUrl, params = redactedParams, autoDecompress = false)
inflateResponse(str)

Or alternatively set headers to Map("Accept-Encoding" -> "gzip") if the server supports it, and avoid the deflate nonsense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant