{"id":7986,"date":"2015-11-15T02:16:01","date_gmt":"2015-11-15T02:16:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/15\/retrofit-post-request-w-basic-http-authentication-cannot-retry-streamed-http-body-open-source-projects-square-retrofit\/"},"modified":"2022-08-30T15:03:05","modified_gmt":"2022-08-30T15:03:05","slug":"retrofit-post-request-w-basic-http-authentication-cannot-retry-streamed-http-body-open-source-projects-square-retrofit","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/15\/retrofit-post-request-w-basic-http-authentication-cannot-retry-streamed-http-body-open-source-projects-square-retrofit\/","title":{"rendered":"Retrofit POST request w\/ Basic HTTP Authentication: &#8220;Cannot retry streamed HTTP body&#8221;-open source projects square\/retrofit"},"content":{"rendered":"<p>I&#8217;m using Retrofit to do a basic POST request, and I&#8217;m providing a basic @Body for the request.<\/p>\n<pre><code>@POST(\"\/rest\/v1\/auth\/login\")\nLoginResponse login(@Body LoginRequest loginRequest);\n<\/code><\/pre>\n<p>When I&#8217;m building the interface for Retrofit I&#8217;m providing my own custom OkHttpClient, and all that I&#8217;m doing to it is adding my own custom authentication:<\/p>\n<pre><code>    @Provides\n    @Singleton\n    public Client providesClient() {\n        OkHttpClient httpClient = new OkHttpClient();\n\n        httpClient.setAuthenticator(new OkAuthenticator() {\n            @Override\n            public Credential authenticate(Proxy proxy, URL url, List challenges) throws IOException {\n                return getCredential();\n            }\n\n            @Override\n            public Credential authenticateProxy(Proxy proxy, URL url, List challenges) throws IOException {\n                return getCredential();\n            }\n        });\n\n        return new OkClient(httpClient);\n    }\n<\/code><\/pre>\n<p>This works great when I&#8217;m sending requests directly with OKHttp, and other GET requests with retrofit but when I use retrofit to do a POST request I get the following error:<\/p>\n<pre><code>Caused by: java.net.HttpRetryException: Cannot retry streamed HTTP body\n            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:324)\n            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:508)\n            at com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)\n            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:94)\n            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:49)\n            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:357)\n            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:282)\n            at $Proxy3.login(Native Method)\n            at com.audax.paths.job.LoginJob.onRunInBackground(LoginJob.java:41)\n            at com.audax.library.job.AXJob.onRun(AXJob.java:25)\n            at com.path.android.jobqueue.BaseJob.safeRun(BaseJob.java:108)\n            at com.path.android.jobqueue.JobHolder.safeRun(JobHolder.java:60)\n            at com.path.android.jobqueue.executor.JobConsumerExecutor$JobConsumer.run(JobConsumerExecutor.java:172)\n            at java.lang.Thread.run(Thread.java:841)\n<\/code><\/pre>\n<p>I&#8217;ve played around with it. If I remove the authentication, and point to a server that doesn&#8217;t require the authentication, then it works fine.<\/p>\n<ol>\n<li>So I must be sending the information.<\/li>\n<li>Getting the Authentication challenge request.<\/li>\n<li>Responding to the challenge request.<\/li>\n<li>Trying to resend the request again, and then the error is being thrown.<\/li>\n<\/ol>\n<p>Not sure how to get around this. Any help would be wonderful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using Retrofit to do a basic POST request, and I&#8217;m providing a basic @Body for the request. @POST(&#8220;\/rest\/v1\/auth\/login&#8221;) LoginResponse login(@Body LoginRequest loginRequest); When I&#8217;m building the interface for Retrofit I&#8217;m providing my own custom OkHttpClient, and all that I&#8217;m doing to it is adding my own custom authentication: @Provides @Singleton public Client providesClient() { [&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-7986","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7986","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=7986"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7986\/revisions"}],"predecessor-version":[{"id":8722,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7986\/revisions\/8722"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}