Salesforce Integration– Apex REST API basics

REST API or Representational state transfer(REST) or RESTful web service basics

As per previous post REST API is based on HTTP callouts. Force.com platform provides multiple built in classes to work with HTTP services and create services like GET, POST, PUT and delete.

Note: HTTP classes also useful to integrate SOAP based services as an alternate option to generate code from WSDL.

Below are the pre-defined HTTP classes.

HTTP Class: This class is useful to initiate HTTP request & Response.

HttpRequest Class: This class supports Request types like GET, POST, PUT, DELETE, TRACE, CONNECT, HEAD, OPTIONS. Supports Request headers, redirects, read and connection timeouts and Content of the message body.

HttpResponse Class: This class is useful to handle the HTTP response returned by HTTP like HTTP status code, Response headers and content of the response body.

Example/Sample Class to understand basic of HTTP callouts:

public class HttpCalloutExampleClass {
// Pass in the endpoint to be used using the string url
public String getCalloutResponseContents(String url) {
// Instantiate a new http object
Http ht = new Http();
/* Instantiate a new HTTP request, specify the method (GET) as well as the endpoint */
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(‘GET’);
// Send the request, and return a response
HttpResponse res = ht.send(req);
return res.getBody();
}
}

Leave a Reply