1

I am making a HTTP POST callout from Salesforce. I am getting a HTTP 200 response code from the remote server but I am not getting the response body in the response. However, when I am using the same request body to send the data from postman, I am getting the response body.

Here's the Salesforce code I am using:

HttpRequest req = new HttpRequest();            
req.setMethod('POST');

req.setEndpoint('https://ZXXXXXXXX/api/ZXXXXXXXX');
String username = 'ZXXXXXXXX';
String password = 'XXXXXXXXXXXXXXX';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type', 'application/json');

req.setBody(JSON.serialize(lstOfObjectIWantToSend));


System.debug('json serialized object: 
'+JSON.serialize(lstOfObjectIWantToSend));

HttpResponse res = new Http().send(req);

System.debug('Response from Server in Salesforce: '+res);

I am using the reference code depicted here: How to make a post request to some other server from apex controller

Am I doing something wrong here?


First Edit: In the logs, I am printing the entire response object, this is what I see:

USER_DEBUG [50]|DEBUG|Response from Dynamics in Salesforce: System.HttpResponse[Status=OK, StatusCode=200]

  • Why are you saying you are not getting response? did you print the response body? Try to print res.getBody() and see what it prints. – realfire Mar 12 '18 at 23:25
  • @datasert I am not getting response body. I am getting only response code and response status. – Json Bourne Shell Mar 12 '18 at 23:33
1

I got the response when I used res.getBodyAsBlob().toString();

  • I am also facing the same issue.When i try to use like above it saying argument cannot be null – Pavan tej Aug 7 '18 at 16:24
  • Do System.debug() for all possible response methods. One of them should have a response. – Json Bourne Shell Aug 8 '18 at 23:44
0

When you print the response object as is, Salesforce doesn't log the body as body could be huge. Try logging the body explicitly.

System.debug('Response from Server in Salesforce: '+res.getBody());
  • Since I wasnt getting anything in res.getBody() I printed the entire res object. But to my point, I am not getting res.getBody() even if I print it explicitly – Json Bourne Shell Mar 12 '18 at 23:35
  • If you already tried to print body, then Server you calling must be returning different response than when you post via Postman. Because of fundamental nature of http callouts, I don't think Salesforce would be any issue here. I would first try to proxy the call via some third party and see what shows up. – realfire Mar 12 '18 at 23:37
  • I got the response when I used res.getBodyAsBlob().toString() – Json Bourne Shell Mar 13 '18 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.