Kong / unirest-java

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

Null body on GET request #215

Closed pablof300 closed 5 years ago

pablof300 commented 7 years ago

I am using the version 1.4.9 in Java to accomplish a get request (http://api.bitcoincharts.com/v1/trades.csv?symbol=krakenUSD&start=1504121460)

using this code:

HttpRequest r = Unirest.get("http://api.bitcoincharts.com/v1/trades.csv").queryString("symbol", "krakenUSD").queryString("start", "1303100000");

However, when I get the body of this request it appears as null. When I try this request myself, using Postman, it is successful?

rkaran4 commented 6 years ago

Request is not sent upon construction of HttpRequest object using Unirest.get() method. Try using this: Unirest.get("http://api.bitcoincharts.com/v1/trades.csv").queryString("symbol","krakenUSD").queryString("start", "1504121460").asString().getBody();

When asString() is called on HttpRequest object, then request is sent and the body field is populated with the data received from server.

azakordonets commented 5 years ago

@rcodekaran yeah but that doesn't solve the problem. If there's no body in response, then it's populated as null an then asString() method will throw NullPointerException, which sucks

ryber commented 5 years ago

This is fixed in an upcoming version. Ill tag this and close it when it's out.

ryber commented 5 years ago

Hello, this should be resolved or addressed in Unirest 2. Please see the upgrade guide for important changes.