+ Start a Discussion
arparp 

Consume a third party REST Api in salesforce

All,

 

I have a requirement where I have to call a third party REST Api in my salesforce app.

 

I have been reading and I see all posts show how to create a REST call for outside world to consume but what I want is something simple call the ouside world's API. How do I proceed about it?

I see that I have to add the external url in Remote site settings and what else code do I need to craete.

 

Thanks

 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

You need to use HTTP callout 

 

http://salesforce.stackexchange.com/questions/5182/how-to-make-a-post-request-to-some-other-server-from-apex-controller/5209#5209

 

public class AuthCallout {

 public void basicAuthCallout(){
 HttpRequest req = new HttpRequest();
 req.setEndpoint('http://www.yahoo.com');
 req.setMethod('GET');

 // Specify the required user name and password to access the endpoint 

 // As well as the header and header information 


 String username = 'myname';
 String password = 'mypwd';

 Blob headerValue = Blob.valueOf(username + ':' + password);
 String authorizationHeader = 'BASIC ' +
 EncodingUtil.base64Encode(headerValue);
 req.setHeader('Authorization', authorizationHeader);

 // Create a new http object to send the request object 

 // A response object is generated as a result of the request   


 Http http = new Http();
 HTTPResponse res = http.send(req);
 System.debug(res.getBody());
  }
 }

  An example call for GET

 

refer the above link i shared for POST request.Also you may need to check authentication you apply

Ramesh Naidu PRamesh Naidu P
Hi Mohith,

above code is not working. it gives error  No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://ramjiva.ap2.visual.force.com' is therefore not allowed access.