Everything you need to know about REST callouts in Apex

The Salesforce Apex callouts enable the integration of Apex with external services using SOAP and WSDL, or HTTP services (REST services); with each callout calls the external Web service or sends an HTTP request via Apex code to later receive the response on it.

 

As the REST callouts are based on HTTP and each request has its own HTTP method, specifying its desired type of action and an endpoint.

Prerequisites:

As Salesforce prevents calls to unauthorized network addresses so the site needs to be registered on the Remote Site Settings page before any Apex Callout or it fails.

 

Also, one doesn’t need to configure remote site settings if a named credential (which mentions the URL of the callout endpoint and its required authentication) is specified in the callout as the endpoint.

Invoking HTTP Callout:

There are several built-in classes in Apex that works with HTTP services which are used in the integration with REST based services and create HTTP request like GET, POST, PUT, and DELETE. Alternatively, you can also integrate it to SOAP based web services by generating Apex code from a WSDL. Instead, to start with WSDL, more can be done with the construction of SOAP for the request and response using HTTP classes.

HTTP requests and terminology explained:

 

  • GET: It helps in retrieving resources identified by the URL.
  • PUT: It allows to create or replace the resource that is sent to the request body.
  • DELETE: It deletes the resource that is identified by the URL.
  • POST: It creates a resource or post data to the server.
  • URI: It specifies the endpoint address where the service is located.
  • JSONParser: It is a built-in class that converts string to an object.

Note:

As the Apex test methods do not support callouts so you need to use mock callouts to test the callouts; which do not sends requests to the endpoint rather the Apex runtime looks up the specified response in the mentioned static resource and returns it.

 

The mock callouts can be used by either implementing an interface or using static resources.

Invoking HTTP GET Request: Getting Data from a Service:

The simplest HTTP method is the GET request which allows the sender to retrieve data about any resource form the server, enabling the server to receive and process the request and return it with the desired information to the recipient.

 

Similar to navigating any address on the browser. Visit any web page and allow the browser to navigate you to your specified HTML page while GET request performs in the background with the response object as the result.

 

This service responds in JSON format which is a string and can be converted into an object using built-in JSONParser class.

 

Invoking HTTP POST Request: Sending Data to a Service:

Another common HTTP method is its POST request which allows sending data to a service. For example, when you comment on any video your browser makes a POST request to the web service to submit the data.

 

The comment gets added as a JSON string to the request body and the Content – Type header is set to inform the service that the sent data is in the JSON format and processed appropriately; resulting in the list of comments with the one you added in the response.

 

Note: The status code returns 201 when the request is process successfully and a resource has been created else the response is sent to the debug log when anything other then 201 is returned.

Similar to GET and POST requests, other HTTP methods are performed. Start writing your own code and become an expert is REST callouts in Apex.

 

If you need any help in it, reach out to us at [email protected] We’ll be glad to help you!

Our Location worldwide
India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
SR Tower 2nd Floor Hydel Gate Haldwani Uttarakhand 263126 +91-5946359996
USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818