How to consume third party Rest API From salesforce

Bynayakdillip

How to consume third party Rest API From salesforce

Rest API:Representational State Transfer

Let’s discuss some stubs about Salesforce Integration.

How to consume third-party Rest API From salesforce.

here I am going to explain how we send request from salesforce to third-party rest API. Then
get a response from third-party API.

the simplest request is a GET request(HTTP Method).

GET–>GET methods to help retrieve data from identified by a URL.

Example:-

First, need to add your third-party REST API to the remote site settings in Salesforce org.Once done, we need to create apex code to retrieve records.

I have found below Third party URL for testing point of view

https://jsonplaceholder.typicode.com/

Step:-1

setup–>Remote Site Setting

Step:-2

Add URL

https://jsonplaceholder.typicode.com

Step:3
I. Open the Developer Console from the Setup gear (Setup gear icon).
II. On the Developer Console, select Debug | Open Execute Anonymous Window.

 Http http=new Http();
 HttpRequest request=new HttpRequest();
 request.setEndpoint('https://jsonplaceholder.typicode.com/users');
 request.setMethod('GET');
 HttpResponse response=http.send(request);
 if(response.getStatusCode()==200)
  {
            list<object> results=(list<object>)JSON.deserializeUntyped(response.getBody());
          
            for(object user:results)
            {
                system.debug('user'+user);
            }
   }

III. Select code, and then click Execute.
IV. After the debug log opens, select Debug Only to view the output of the System.debug statements.

The user detail of user is displayed.




Class Example:-

public class jsonplaceholderAPI {
   
public  static list<object> GetUsers()
    {
        Http http=new Http();
        HttpRequest request=new HttpRequest();
        request.setEndpoint('https://jsonplaceholder.typicode.com/users');
        request.setMethod('GET');
        HttpResponse response=http.send(request);
        list<object> results=new list<object>();
        if(response.getStatusCode()==200)
        {
           results=(list<object>)JSON.deserializeUntyped(response.getBody());
           
        }
        return results;
    }
}

About the author

nayakdillip administrator

Leave a Reply