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();
}
}

Salesforce Integration – Introduction

Salesforce Integration/Apex Integration

Salesforce Integration/Apex Integration Introduction

Apex Callout: A callout makes a call to external web service or sends HTTP request to from Apex Code, and then receives the response.

An Apex callout helps you to integrate Apex code with an external web service.

Below are the two types Apex Callouts: 

SOAP: Web service callout to SOAP web service use XML. This require a WSDL document for code generation.

REST: HTTP callouts to services typically use REST with JSON.

Both SOAP & REST callouts are similar in terms of sending a request to a service and receiving a response. WSDL-based callouts apply to SOAP Web services, HTTP callouts can be used with any HTTP service, either SOAP or REST.

Remote site administration and security: Register you external site in salesforce remote site settings before callout can call external site, otherwise callouts will fail. Salesforce prevents calls to unauthorized network addresses.

How to add remote site setting?

To add remote site setting go to setup -> remote site setting Click New remote Site Setting enter remote site setting name and URL click on save.

Salesforce Integration

Remote Site Setting 1