function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rafael.Martins.SantosRafael.Martins.Santos 

How create a Rest Client in SF that make a Get from another system?

Hi,
I'm trying consume a Rest API of another system using apex, but I don't know how do that.
I'm reading and following the steps of the Trailhead (REST and SOAP Integration) but this is not clearly for me.

So my ideia is:
Consume Rest API from another system and make a GET request (Thats include the authentication of the another system).
I don't know which exactly code the apex accept, so I'm totaly lost.
if someone can help me, telling me the steps I need to do and some GET code for example that include authentication, will help me a lot.

Someone can help me with this issue?

Best Regards
Rafael
Saurabh BSaurabh B
Hi Desenv,

The answer depends upon type of API you are trying to reach. For example,
  • Some API offer one way authentication - which means after you send userid and password/token, you will directly get response (data) from the server
  • Some offer two way authentication, which means after you send userid and password/token, their API server will return another security token/or Session ID. You use that security token or security token/or Session ID to make 2nd callout and that will give you the actual data.

You should understand the authentication process of the API you are trying to communicate. I would suggest, try using tools like POSTMAN or cURL (Postman is lot easier). It will allow you the test the API that you are trying to use.

See these good articles which will help you with the sample code,

http://blog.jeffdouglas.com/2009/03/16/restful-web-service-callout-using-post/
http://cyukt.com/learn/salesforce-rest-api-callout-consume-external-rest-api/

Also, when you say "I'm trying consume a Rest API of another system using apex," , are you trying to establish connection between one Salesforce org to another Salesforce. If yes, try using Salesforce to Salesforce funtionality. Its easier way to establish connnection between two salesforce orgs. If you want to do that via Rest API, see below link -

http://salesforce.stackexchange.com/questions/25021/how-can-i-integrate-one-sfdc-org-to-another-sfdc-using-rest-api
http://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/

Hope this helps! :)
 
Rafael.Martins.SantosRafael.Martins.Santos
Hi Saurabh,
Thanks for your help.
 - The other System that I'm saying is ServiceNow, so the integration will be of the Salesforce and ServiceNow.
 - I already consume the Rest API of ServiceNow in Postman and Java, and I did a GET and POST for test.
 - But I need do the same thing in APEX. I don't know if all the code I did in Java works in APEX.
 - I don't know if exist some doc that give me all the code that the APEX accept.(Some type of library of APEX).
 - The authentication accept a type "BASIC AUTHeNTICATION".
 
I write this to be more especific of what I want, but I thing these two links that you sent to me will help me, if not I came back :)

Thanks again Saurabh

 
Saurabh BSaurabh B
Hi Desenv,
I`m glad it helped.
  • If you are able to consume the API using cURL or Postman, thats a good news. You can now replicate similar methods in Salesforce.
  • Though Apex and Java have similarities, Java code will not work in Apex.
  • If its a Basic Authentication, you can try using a code like below,
 
public class WebServiceCallout {
    
    @future(callout=true)
    public static void sendCalloutREST(String jsonString){
        try{
            String endPointURL = 'https://samplesite.com/samplerest';
            String userName = 'samuser';
            String password = 'Sam@789';
            
            // Specify the required user name and password to access the endpoint
            // As well as the header and header information
            Blob headerValue = Blob.valueOf(userName + ':' + password);
            String authorizationHeader = 'BASIC ' +
                EncodingUtil.base64Encode(headerValue);
            
            Httprequest request = new HttpRequest();
            Http http = new Http();
            
            request.setMethod('POST');
            request.setEndpoint(endPointURL);
            request.setHeader('Content-Type', 'application/json');
            // Header info with remote server user name and password
            request.setHeader('Authorization', authorizationHeader);
            //Check the client certificate
            request.setClientCertificateName('Sample-Rest-Self-Signed');
            // timeout in milliseconds       
            request.setTimeout(120000);
            request.setBody(jsonString);          
            
            //Making call to external REST API
            HttpResponse response = http.send(request);  
            
            System.debug('responseBody: '+response.getBody());
        }catch(Exception e){
            System.debug('Error::'+e.getMessage());
        }
        
    }
}

Also, keep in mind you below things,
1) You will need to parse your JSON response in Apex so that you can use it. You can use this tool which will generate an Apex class for you to parse your response. All you have to do is pass your JSON query and click on generate.
2) You will need to write test class for your Apex callouts. Use Apex HttpCalloutMock Interface to replicate behaviour of your Webservice API.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

Hope this helps.

Please mark this as Best Answer if it helps you!
Rafael.Martins.SantosRafael.Martins.Santos
Hi Saurab,
Can you post an example of GET?
The POST I did and works.

 
Ankit Arora 29Ankit Arora 29
@everyone

Hi All,

I need some help. I have received APIs from a company that has API Username and Password and other details in JSON format. I have to POST some data on their server from Salesforce and then, later on, GET the data into the Salesforce. I am not sure how to do it. Can you please help me?

I tried the above code but I am still confused.
Thank you

Regards,
Ankit