Friday 7 April 2017

How to Test Salesforce REST API

                   How to Test Salesforce REST API

To Test Salesforce Rest API First we need to create Connected App in Salesforce.Once we created Connected App it will generate consumer or client id and consumer secret.



After that we need to create Access-token using below steps.

1.grant_type=password&client_id=YourClientId&client_secret=YourClientSecret&username=YourUserName&password=YourPassword+securitytoken

EX:
client_id =  3MVG9ZL0ppGP5UrB.7GK_luxRT7StxC8dM37EZzuyrj0LMr_TaiMZatpDKASHUikAvgczZCo1ONfmzhV5LER1

client_secret = 1328342625760846904

username =  username

password=YourPassword+securitytoken

Content-Type : application/x-www-form-urlencoded

To make HTTP Requests we are using https://www.hurl.it/  or SOAP UI tool.


Here we will get response like below format:

{"access_token": "00D28000001xtTR!AQkAQO.ZKKI4ccUpsQLzm.RpgI4HxLuMU8vqrpmpku_DEc3_aHMs9ar60o_l2HR9Vc2cF3IbFS8jC5UDD38h3CmXvBX6ue42","instance_url": "https://ap2.salesforce.com","id": "https://login.salesforce.com/id/00D28000001xtTREAY/00528000005dGo3AAE","token_type": "Bearer","issued_at": "1491551856783","signature": "RxPY7+SXQEyaIzTW3B5c1N/0C3ciwhojKNbULX9Xawo="}

Now we will test Rest WS using above access token.

Sample RestClass:
@RestResource(urlmapping = '/showAccounts/*')
global class RestExample {
   
    @HttpGet
    global static list<account> getAccounts(){
        list<account> lstAccounts = new list<account>();
        RestRequest request = RestContext.request;
        system.debug('request >>>' + request);
        string accName = request.params.get('name');
        system.debug('==accName==='+accName);
        lstAccounts = [select id,name,phone from account where name =: accName];       
        system.debug('===result==='+lstAccounts);
        return lstAccounts;
    }
   
}



Using Below Extensions also we can test REST WSs.

Advanced REST client - Chrome Web Store

Postman - Chrome Web Store


No comments:

Post a Comment

Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...