Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.59k stars 593 forks source link

Can't disable GET request caching? #369

Closed mgrubent closed 3 years ago

mgrubent commented 3 years ago

Describe the bug Unirest.get(url).queryString(queryStrings).asJson() appears to return a cached response, even when preceded by Unirest.config().cacheResponses(false)

To Reproduce Steps to reproduce the behavior:

Unirest.config().cacheResponses(false);
HttpResponse<JsonNode> response1 = Unirest.get(url).queryString(queryStrings).asJson();
// Omitted: Change the world here, maybe POST with Unirest.post
HttpResponse<JsonNode> response2 = Unirest.get(url).queryString(queryStrings).asJson();

// Should be false, but will be true
System.out.println(response1.getBody().toString().equals(response2.getBody().toString()));

If, for instance, OkHttpClient is used instead, the correct and expected different responses are obtained.

Expected behavior After the world changes, the body of the response returned by Unirest.get should change

Screenshots N/A

Environmental Data:

Additional context N/A

mgrubent commented 3 years ago

Unirest.primaryInstance().config().cacheResponses(false); also appears to have no effect.

The following does seem to be a workaround, though it seems heavy-weight

UnirestInstance instance1 = Unirest.spawnInstance();
instance1.config().cacheResponses(false);
HttpResponse<JsonNode> response1 = instance1.get(stateUrl).queryString(queryStrings).asJson();

HttpResponse<JsonNode> response2 = Unirest.get(stateUrl).queryString(queryStrings).asJson();
ryber commented 3 years ago

There are extensive tests showing the cache NOT enabled. are you sure you don't have something else going on? Is there some process somewhere else setting it to true on the instance. There is no CacheManger by default, so I assume some other process is enabling it on the instance.

I added some tests explicitly with json:

https://github.com/Kong/unirest-java/commit/314679633bbba9110b0f6f78029841686906dbcf#diff-ab2b7caed87334911818ecc1a66b09d2

mgrubent commented 3 years ago

Yep, there was indeed a lurking call to Unirest.config().reset().

Thanks for the quick response Ryan, I'll close this issue since I think this issue is specific to my usage of Unirest.