[SalesForce] HttpResponse body is empty even on successful request

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);

Best Answer

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.

Related Topic