[SalesForce] Rest API working well from Postman doesn’t work from Salesforce

I am beginner to REST APIs. I had a REST api used to login into some third party application. When used from Postman extension of Chrome it works well. However throws some generic error when used from Salesforce execute anonymous:

Httprequest request = new HttpRequest();
Http http = new Http();
request.setMethod('POST');
request.setEndpoint('http://someendpoint.com/service');
request.setTimeout(120000); 
String jsonString='{"login":"serviceusername","password":"passwordtologin","clientname":"101","clientappid":"202","clientappkey":"303"}';   
request.setBody(jsonString);          
System.debug('====='+request.getbody());     
//Making call to external REST API
HttpResponse response = http.send(request);  
System.debug('====='+response.getbody());

The only thing that I followed before using this api was including endpoint in remote site setting.I keep getting a generic response error saying invalid appkey/clientname. The reason I am saying its generic is because error remains same even when I use invalid username password. Am I missing something ?

Postman Screenshot:
enter image description here

Best Answer

I would use the generate code button in postman, select the cUrl type. Take a look at the headers it generates on the working Postman version (the parameters with the -H) and add them to your apex using the setHeader method. Something like:

http.setHeader('content-type','application/Json');
Related Topic