'How to remove header "accept-encoding:gzip" with okhttp

I am using OkHttp 3 and trying to remove accept-encoding header completely from requests made by OkHttpClient.

I use .removeHeader("accept-encoding") on the Request object, but the header still appears in the request.

Is there a way to remove the header completely and not just replace it with a different value?



Solution 1:[1]

You can remove it using a network interceptor.

final class AcceptEncodingInterceptor implements Interceptor {
  @Override public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();
        .newBuilder()
        .removeHeader("Accept-Encoding")
        .build();
    return chain.proceed(request);
  }
}

The the interceptor like this:

new OkHttpClient.Builder()
    ...
    .addNetworkInterceptor(new AcceptEncodingInterceptor())
    .build();

Note that is has to be addNetworkInterceptor, simply addNetworkInterceptor does not work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lii