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

Viewed 725

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?

1 Answers

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.

Related