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
OTDev2OTDev2 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Chris DaviesChris Davies

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

Chris DaviesChris Davies

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,

This was selected as the best answer
OTDev2OTDev2

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

Chris DaviesChris Davies

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:

  1. Click New Remote Site.
  2. Enter a descriptive term for the Remote Site Name.
  3. Enter the URL for the remote site.
  4. To allow access to the remote site regardless of whether the user's connection is over HTTP or HTTPS, select the Disable Protocol Security checkbox. When selected, Salesforce can pass data from an HTTPS session to an HTTP session, and vice versa. Only select this checkbox if you understand the security implications.
  5. Optionally, enter a description of the site.
  6. Click Save to finish, or click Save & New to save your work and begin registering an additional 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

 

OTDev2OTDev2

Thank you very much, Chis.