Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude default headers set by OkHttp library

Tags:

android

okhttp

In recent versions of OkHttp library, headers like "Accept-Encoding" and "User-Agent" are added automatically if you don't provide them by yourself.

Is there a way to disable this feature?

like image 523
Vitaly Zinchenko Avatar asked Jul 09 '26 19:07

Vitaly Zinchenko


1 Answers

Strip 'em with a Network Interceptor.

client.networkInterceptors().add(new Interceptor() {
  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request()
        .newBuilder()
        .removeHeader("Accept-Encoding")
        .removeHeader("User-Agent")
        .build();
    return chain.proceed(request);
  }
});
like image 123
Jesse Wilson Avatar answered Jul 13 '26 14:07

Jesse Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!