4

I am doing a Http request to a web service and I get status 200 success. However when I try to read the response body (response.getBody()) it comes as empty.

When I do response.toString() I get System.HttpResponse[Status=OK, StatusCode=200] When I do response.getBodyAsBlob() I get null. Basically I am not able to read the response body even when the request is a success.

However when I copy and past the request endpoint in the browser or Hurl.it it gives back the expected JSON response.

Any idea on why I am not able to read the response body?

------------------- Update, the code --------------------------------

HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
req.setHeader('api_token', apitoken);
Http http = new Http();
HTTPResponse response = http.send(req);
12
  • 1
    can you share your code?
    – Anshul
    Commented Jun 19, 2015 at 21:29
  • just updated with code. Commented Jun 19, 2015 at 21:33
  • 1
    Have you tried response.getBody()
    – Eric
    Commented Jun 19, 2015 at 21:35
  • Yes, its coming as empty. Commented Jun 19, 2015 at 21:37
  • 1
    The thing is when I do the same thing with Hurl.it it gives me the JSON data. I don't know why it works there and not in my class. Commented Jun 19, 2015 at 21:57

1 Answer 1

4

Finally after analyzing the request sent by the browser and Hurl.it it seems that I was missing a header called Accept-Encoding whose value was 'gzip'.

req.setHeader('Accept-Encoding', 'gzip');

Now it is returning the JSON response it was suppose to.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .