I get it. The idea is to add a custom interceptor that will accept the unpacked response and unzip it βmanuallyβ - to do the same thing that OkHttp will do automatically based on the Content-Encoding header, but without requiring this header.
similar to dis:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .addInterceptor(new UnzippingInterceptor()); OkHttpClient client = clientBuilder.build();
And the interceptor is like dis:
private class UnzippingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); return unzip(response); } }
And the unzip function is like dis:
// copied from okhttp3.internal.http.HttpEngine (because is private) private Response unzip(final Response response) throws IOException { if (response.body() == null) { return response; } GzipSource responseBody = new GzipSource(response.body().source()); Headers strippedHeaders = response.headers().newBuilder() .removeAll("Content-Encoding") .removeAll("Content-Length") .build(); return response.newBuilder() .headers(strippedHeaders) .body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody))) .build(); }
source share