How to specify a default user agent for okhttp 2.x requests How to specify a default user agent for okhttp 2.x requests android android

How to specify a default user agent for okhttp 2.x requests


You can use an interceptor to add the User-Agent header to all your requests.

For more information about okHttp interceptors see http://square.github.io/okhttp/interceptors/

Example implementation of this interceptor:

/* This interceptor adds a custom User-Agent. */public class UserAgentInterceptor implements Interceptor {    private final String userAgent;    public UserAgentInterceptor(String userAgent) {        this.userAgent = userAgent;    }    @Override    public Response intercept(Chain chain) throws IOException {        Request originalRequest = chain.request();        Request requestWithUserAgent = originalRequest.newBuilder()            .header("User-Agent", userAgent)            .build();        return chain.proceed(requestWithUserAgent);    }}

Test for the UserAgentInterceptor:

public void testUserAgentIsSetInRequestHeader() throws Exception {    MockWebServer server = new MockWebServer();    server.enqueue(new MockResponse().setBody("OK"));    server.play();    String url = server.getUrl("/").toString();    OkHttpClient client = new OkHttpClient();    client.networkInterceptors().add(new UserAgentInterceptor("foo/bar"));    Request testRequest = new Request.Builder().url(url).build()    String result = client.newCall(testRequest).execute().body().string();    assertEquals("OK", result);    RecordedRequest request = server.takeRequest();    assertEquals("foo/bar", request.getHeader("User-Agent"));}


In case anyone is looking for this working with OkHttp 3 and in Kotlin:

val client = OkHttpClient.Builder()    .addNetworkInterceptor { chain ->      chain.proceed(          chain.request()              .newBuilder()              .header("User-Agent", "COOL APP 9000")              .build()      )    }    .build()


OkHttp v2.1 which is set to be released in the next few weeks will automatically set a User-Agent header if one is not already set.

As of now there isn't a good way to add this header to every request in a centralized way. The only workaround is to include the header manually for every Request that is created.