{"id":7740,"date":"2015-10-19T01:17:30","date_gmt":"2015-10-19T01:17:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/19\/how-to-specify-a-default-user-agent-for-okhttp-2-x-requests-open-source-projects-square-okhttp\/"},"modified":"2015-10-19T01:17:30","modified_gmt":"2015-10-19T01:17:30","slug":"how-to-specify-a-default-user-agent-for-okhttp-2-x-requests-open-source-projects-square-okhttp","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/19\/how-to-specify-a-default-user-agent-for-okhttp-2-x-requests-open-source-projects-square-okhttp\/","title":{"rendered":"How to specify a default user agent for okhttp 2.x requests-open source projects square\/okhttp"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/328ab95c8fa6594e6653266001ba8f1e?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>josketres<\/strong><\/p>\n<p>You can use an interceptor to add the User-Agent header to all your requests.<\/p>\n<p>For more information about okHttp interceptors see https:\/\/github.com\/square\/okhttp\/wiki\/Interceptors<\/p>\n<p>Example implementation of this interceptor:<\/p>\n<pre><code>\/* This interceptor adds a custom User-Agent. *\/\npublic class UserAgentInterceptor implements Interceptor {\n\n    private final String userAgent;\n\n    public UserAgentInterceptor(String userAgent) {\n        this.userAgent = userAgent;\n    }\n\n    @Override\n    public Response intercept(Chain chain) throws IOException {\n        Request originalRequest = chain.request();\n        Request requestWithUserAgent = originalRequest.newBuilder()\n            .removeHeader(\"User-Agent\")\n            .addHeader(\"User-Agent\", userAgent)\n            .build();\n        return chain.proceed(requestWithUserAgent);\n    }\n}\n<\/code><\/pre>\n<p>Test for the UserAgentInterceptor:<\/p>\n<pre><code>public void testUserAgentIsSetInRequestHeader() throws Exception {\n\n    MockWebServer server = new MockWebServer();\n    server.enqueue(new MockResponse().setBody(\"OK\"));\n    server.play();\n    String url = server.getUrl(\"\/\").toString();\n\n    OkHttpClient client = new OkHttpClient();\n    client.networkInterceptors().add(new UserAgentInterceptor(\"foo\/bar\"));\n    Request testRequest = new Request.Builder().url(url).build()\n    String result = client.newCall(testRequest).execute().body().string();\n    assertEquals(\"OK\", result);\n\n    RecordedRequest request = server.takeRequest();\n    assertEquals(\"foo\/bar\", request.getHeader(\"User-Agent\"));\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>josketres You can use an interceptor to add the User-Agent header to all your requests. For more information about okHttp interceptors see https:\/\/github.com\/square\/okhttp\/wiki\/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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7740","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7740"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7740\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}