2

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:

  1. 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
    
  2. Run the server and make it available.

  3. 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)
    
  4. 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)

Ana
  • 73
  • 7
  • Why do you think it's a problem of HTTP? Possibly it's a bug of a server. Try to test queries with Postman. – CoolMind Feb 06 '18 at 17:49
  • Previously I've checked with cURL, and also I've checked with latest official version of our Android client which uses Apache HttpClient (I'm rewriting this app to use Retrofit2). Both clients receive expected 400 response code at the end of this scenario. – Ana Feb 07 '18 at 09:18
  • I've also checked with Postman, as you suggested. The behaviour is expected, the Postman client also gets 400 response code at the end of the scenario. – Ana Feb 07 '18 at 10:23

0 Answers0