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]

2
  • 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.
    – metasync
    Mar 12, 2018 at 23:25
  • @datasert I am not getting response body. I am getting only response code and response status. Mar 12, 2018 at 23:33

2 Answers 2

1

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

2
  • I am also facing the same issue.When i try to use like above it saying argument cannot be null
    – Pavan tej
    Aug 7, 2018 at 16:24
  • Do System.debug() for all possible response methods. One of them should have a response. Aug 8, 2018 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());
3
  • 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 Mar 12, 2018 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.
    – metasync
    Mar 12, 2018 at 23:37
  • I got the response when I used res.getBodyAsBlob().toString() Mar 13, 2018 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.