Quick Reference Study Notes for Salesforce REST API Integration (Foundation)

Salesforce REST API Integration

REST callouts are based on HTTP. Each callout request is associated with an HTTP method and an endpoint. The HTTP method indicates what type of action is desired.

The simplest request is a GET request (GET is an HTTP method). A GET request means that the sender wants to obtain information about a resource from the server. When the server receives and processes this request, it returns the request information to the recipient. A GET request is similar to navigating to an address in the browser. When you visit a web page, the browser performs a GET request behind the scenes. In the browser, the result of the navigation is a new HTML page that’s displayed. With a callout, the result is the response object.

 

Some HTTP Methods :-

 

GET - Retrieve data identified by a URL.

POST - Create a resource or post data to the server.

DELETE - Delete a resource identified by a URL.

PUT - Create or replace the resource sent in the request body.

 

In addition to the HTTP method, each request sets a URI, which is the endpoint address at which the service is located. For example, an endpoint can be http://www.example.com

 

When the server processes the request, it sends a status code in the response. The status code indicates whether the request was processed successfully or whether errors were encountered. If the request is successful, the server sends a status code of 200. You’ve probably seen some other status codes, such as 404 for file not found or 500 for an internal server error.

 

Example of the code for REST request:-

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
   // Deserialize the JSON string into collections of primitive data types.
   Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
   // Cast the values in the 'animals' key as a list
   List<Object> animals = (List<Object>) results.get('animals');
   System.debug('Received the following animals:');
   for (Object animal: animals) {
       System.debug(animal);
   }
}

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree