How should I handle gzip in okhttp (decompressing)?
See original GitHub issueFor my current project I’m currently using OkHttp to connect to an API endpoint. Initially I assumed that it would use gzip automatically in the request, but it doesn’t seem to enable it?
Because of this, I added an additional header (.addHeader(“Accept-Encoding”, “gzip”)) to tell the server that I would like a gzip compressed result. After adding this, the response is indeed compressed, however, I am wondering if the okhttp is supposed to handle gzip content automatically (Because the description talks about Transparent GZIP)? For now it seems like I have to manually modify my incoming stream before I can use it in my JSON parser (Using LoganSquare)
Question: Any thoughts on how I could implement this in a better way?
Sample:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("https://api.awesomecompany.com/details/item/1000")
.addHeader("X-TOKEN", "Bearer " + Auth.getInstance(mContext).getToken())
.addHeader("Accept-Encoding", "gzip")
.build();
Response response = client.newCall(request).execute();
if (responseCode == 200) {
// Regular JSON parsing to model
ItemDetailsModel itemDetailsModel = LoganSquare.parse(response.body().byteStream(), ItemDetailsModel.class);
long responseSize = response.body().contentLength(); // Size in bytes
... use my model
// Manually decompress GZIP?
ItemDetailsModel itemDetailsModel = LoganSquare.parse(new GZIPInputStream(response.body().byteStream()), ItemDetailsModel.class);
long responseSize = response.body().contentLength(); // Value is -1, how to get the compressed size?
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
okhttp 3: how to decompress gzip/deflate ... - Stack Overflow
The problem I'm dealing with a host that only accepts a header like: Accept-Encoding: gzip, deflate if I don't add the deflate part...
Read more >Understanding OkHttp's Gzip Compression | Tech Insider
Compression is a simple and effective way to save bandwidth and speed up your Mobile User Interaction. When a user hits your screen, ......
Read more >how to decompress gzip/deflate response... - okhttp 3
I know that okhttp3 library by default it adds the header Accept-Encoding: gzip and decodes the response automatically for us. The problem I'm...
Read more >gzip - OkHttp
Returns a gzip version of the RequestBody, with compressed payload. This is not automatic as not all servers support gzip compressed requests.
Read more >Encoding your HTTP for fun and profit
First, you'll need to make sure you send an Accept-Encoding header, and handle response decompression if the response has a Content-Encoding response header....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @JakeWharton,
thanks for the response. That test certainly works.
I think I misunderstood how “Transparent GZIP” works.
For testing purposes I made a small php script that outputs the value of HTTP_ACCEPT_ENCODING (+ some text to make sure there is enough data to be gzipped). The script would return a gzip version if it was requested, else it would return an uncompressed version. I also made a small Android test project with okhttp that would call this php file.
From what I understand, okhttp will always ask for a gzipped result (testing the php page with curl and the app made this clear). However, unless you’ve manually added the entry .addHeader(“Accept-Encoding”, “gzip”), you won’t see the value “Content-Encoding: gzip” in the header of the Response object. And if I added it manually, I will have to decode the response.body().byteStream() myself.
Sorry for the confusion, you can close this issue.
What makes you say this?
Accept-Encoding: gzip
is added automatically and the gzip’d response will be ungzipped transparently. You can confirm by looking for theTransfer-Encoding: gzip
header on thenetworkResponse()
on theResponse
object.