function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@GM@GM 

REST API - POST service : System.HttpResponse[Status=Internal Server Error, StatusCode=500]

Hi All,
 
I'm trying to hit POST service but getting Internal Server Error (System.HttpResponse[Status=Internal Server Error, StatusCode=500]).
 
Have added the end point host URL to remote site setting. The SOUP UI tool able to hit service without any problem
 
Code:
 
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setEndpoint('http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry');
req.setMethod('POST');
//req.setBody('CountryName='+EncodingUtil.urlEncode('india', 'UTF-8'));
req.setBody('CountryName=india');
req.setCompressed(true); // otherwise we hit a limit of 32000
try {
    res = http.send(req);
    System.debug(res.toString());
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
    System.debug(res.toString());
}
 
Thanks,
GM.
Best Answer chosen by @GM
@GM@GM
Hi Nagendra,

Thanks for the reply. I forgot to update on this issue

Changing content type to application/json worked for me.
req.setHeader('Content-Type','application/x-www-form-urlencoded');

 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi,

First and foremost sincerely regret delayed reply.

I suspect the issue is around the security token and the Trusted IP Ranges in the Network Access Security Controls.

Something to check, look for failed logins under Administration Setup > Manage Users > Login History. If you see the Status message "Failed: API security token required" then the requesting Salesforce IP address isn't trusted.

If the IP address that the login request is coming from isn't trusted Salesforce will reject it unless you append the security token on the end of the password.

So your options are:
  • Modify the Visualforce page to prompt the user to append their security token. You can't get this in Apex code. It is emailed to the user using the Reset my Security Token link.
  • Add the requesting Salesforce servers IP address to the Trusted IP addresses under Administration Setup > SecurSecurityrols > Network Access: Trusted IP Ranges. You could also check Salesforce IP Address to whitelist.

Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue which results in helping others who are really in need of it.

Best Regards,
Nagendra.P
@GM@GM
Hi Nagendra,

Thanks for the reply. I forgot to update on this issue

Changing content type to application/json worked for me.
req.setHeader('Content-Type','application/x-www-form-urlencoded');

 
This was selected as the best answer
@GM@GM
Also, commented this line--req.setCompressed(true); // otherwise we hit a limit of 32000