You need to sign in to do that
Don't have an account?
OTDev2
How to call external web service from Apex Class
Hi,
I'm working on a functionality where I need to call external Web Service to get data from legacy database for use in Apex class method. I wish to know how to include authentication information when making external Web Service call using HttpReqest inside Apex Class method. Is using Http, HttpRequest and HttpResponse is the best way to call external Web Service from Apex Class? It would be really helpful to see some sample code to make call to external Web Service and add authentication information into it.
Thanks,
Prasad.
Here is a sample code on how to make an http callout from an Apex class:
// Instantiate an http protocol class
Http httpProtocol = new Http();
// Instantiate http request class
HttpRequest request = new HttpRequest();
// Set the HTTP verb to GET.
request.setEndPoint(endpointurl);
// Send the HTTP request and get the response.
request.setMethod('GET');
// Create a new http response
HttpResponse response;
response = httpProtocol.send(request);
I suggest that you also check out the Apex Web Services documentation
Hope this helps,
All Answers
Here is a sample code on how to make an http callout from an Apex class:
// Instantiate an http protocol class
Http httpProtocol = new Http();
// Instantiate http request class
HttpRequest request = new HttpRequest();
// Set the HTTP verb to GET.
request.setEndPoint(endpointurl);
// Send the HTTP request and get the response.
request.setMethod('GET');
// Create a new http response
HttpResponse response;
response = httpProtocol.send(request);
I suggest that you also check out the Apex Web Services documentation
Hope this helps,
Hi Chris,
Thank you very much for sample code. I'm able to call the Web Service using the sample code, but I'm getting "Authenticatin Failed" error because Web Service checks for Authentication code.
I would like to know how to specify authentication information when making Web Service call.
Thanks,
Prasad
Before you can make a callout from Apex, you need to create a remote site settings.
Go to Your Name | Setup | Security Controls | Remote Site Settings.
To register a new site:
Once this is done, in my first post you need to create a string 'endpoint url' and assign it the web address of the site that you want to call.
Hope this helps
Thank you very much, Chis.