I'm getting successful response code and wrong response body which corresponds to previous request, when I am expecting Bad request (400) response code and error response body. This happens every time when request call is enqueued after failed connection attempt due to server unavailability.
I'm using Retrofit 2.3.0 version with OkHttp 3.9.1.
The scenario I'm trying is following:
Try to obtain OAuth client credentials from unavailable server. This results in invoking
onFailure()
method of my Retrofit callback as expected.OkHttp: --> POST <my_server_endpoint> OkHttp: Content-Type: application/json; charset=UTF-8 OkHttp: Content-Length: 102 OkHttp: {"clientName":"client_name","grantType":"password refresh_token","owner":"a4yhyh","tokenScope":"production"} OkHttp: --> END POST (102-byte body) OkHttp: - <-- HTTP FAILED: java.net.SocketTimeoutException: timeout BaseCallback: onFailure received. java.net.SocketTimeoutException: timeout
Run the server and make it available.
Try to obtain client credentials again, but for different owner.
OkHttp - --> POST <my_server_endpoint> OkHttp - Content-Type: application/json; charset=UTF-8 OkHttp - Content-Length: 108 OkHttp - {"clientName":"client_name","grantType":"password refresh_token","owner":"uvuguvuv","tokenScope":"production"} OkHttp - --> END POST (108-byte body) OkHttp - <-- 201 Created <my_server_endpoint> (1984ms) OkHttp - Date: Mon, 05 Feb 2018 14:13:47 GMT OkHttp - Content-Type: application/octet-stream OkHttp - Content-Length: 144 OkHttp - {"callback_url":null,"client_secret":"nn59eTalNunqzc_Y6AnuNgvW1EUa","client_name":"a4yhyh_client_name","client_id":"III4ZSLyN4f2AJ_S683QGjepE94a"} OkHttp - <-- END HTTP (144-byte body)
Try to obtain access token using client credentials received from previous response.
OkHttp - --> POST <my_server_endpoint> OkHttp - Content-Type: application/x-www-form-urlencoded OkHttp - Content-Length: 148 OkHttp - client_id=III4ZSLyN4f2AJ_S683QGjepE94a&client_secret=nn59eTalNunqzc_Y6AnuNgvW1EUa&grant_type=password&scope=default&username=uvuguvuv&password=dgghj OkHttp - --> END POST (148-byte body) OkHttp - <-- 201 Created <my_server_endpoint> (6ms) OkHttp - Date: Mon, 05 Feb 2018 14:13:47 GMT OkHttp - Content-Type: application/octet-stream OkHttp - Content-Length: 146 OkHttp - {"callback_url":null,"client_secret":"3ZelXujLNESZffftAjOlZm2H48Aa","client_name":"uvuguvuv_client_name","client_id":"VZHAQOfIuUkhb1JuyPLhNKPubMwa"} OkHttp - <-- END HTTP (146-byte body)
Here is where I expect 400 response code since I'm providing random username and password in the access token request, but instead I'm getting 201 response code and response body that corresponds for previous client credentials request.
I have tried setting retryOnConnectionFailure(false)
when building OkHttp client, and cancelling the call in onFailure()
implementation of my Callback, but nothing seems to work.
I have checked on the server side and it properly returns 400 response code for the access token request.
Did someone encounter this issue? Any ides of how to overcome it? Thanks in advance.
Edit: I've done some research and found out that my problem is similar to following issues: https://github.com/square/okhttp/issues/2394 - although it is stated that the issue is resolved, some people are still facing problems (see https://github.com/OpenFeign/feign/issues/401 and https://github.com/square/okhttp/issues/3830)