본문 바로가기

개발자정보

Postman를 이용한 Salesforce Apex REST APIs 사용 방법

반응형

In this blog, I would like to discuss how to test the salesforce apex rest APIs using postman which is one of the well-known rest client available in the market.

Prerequisites

Download and Install postman application. Use the link given below to download the application.

https://www.getpostman.com/downloads/

Download Page of Postman application

Develop your custom apex rest services. Here I have created two apex service APIs for accessing the account using the account id and other for creating a new account. For more details on the apex rest services, make use of this trailhead module.

  @RestResource(urlMapping='/Accounts/*')
  global with sharing class AccountManager {
   
  @HttpGet
  global static Account getAccountById() {
  RestRequest request = RestContext.request;
  String accountId = request.requestURI.substring(
  request.requestURI.lastIndexOf('/')+1);
  Account result = [SELECT Name,AccountNumber,Type,BillingAddress
  FROM Account
  WHERE Id = :accountId];
  return result;
  }
   
  @HttpPost
  global static ID createAccount(String Name,String accountNumber) {
  Account thisAccount= new Account(
  Name=Name, AccountNumber = accountNumber);
  insert thisAccount;
  return thisAccount.Id;
  }
  }

Create A Connected App

We need to create a connected app for external application to access our apex REST service APIs. First of all, create a connected app in Salesforce with oAuth2 enabled.
Follow the below navigation to see the existing apps with option to create a new connected app.

Setup > Create > Apps

list of apps

To Authorise the external request in Salesforce can be done only through building a connected app. Here, I am creating a connected app named MyRestApp to authorise the external application for accessing the custom rest services defined using apex (see prerequisites section for the code).

Click on the new button in the connected app section, then you will get a screen same as below. Fill up the mandatory app name and contact email fields.

New Connected App

Enable the OAuth settings checkbox to enhance the security of the external connection to salesforce apex rest services.

  • Callback URL  <your org address>/services/oauth2/callback
  • OAuth Scopes – Select the scope based on your requirement, here I am giving FULL ACCESS (there won’t be any restriction for this request which can access any of the content in our org).

Once you save the details then it redirects you to the app details page which describes the client id and client secret along with other option to enhance the security.

Send Request From POSTMAN

Get Access Token

Fill the below details in the request.

  • Request Type  POST
  • url – <org address>/ services/oauth2/token
  • params 
    • grant_type  password
    • client_id – <consumer key of the connected app>
    • client_secret – <consumer secret of the connected app>
    • username – <salesforce username>
    • password – <salesforce password><security token>

Make sure that, you are included the security token in the password parameter otherwise salesforce will throw an invalid grant_type error.

As you can see in the below image, we received the access token in the response body and use this access token for further requests.

Get Account Using AccountId

Now, let’s use your apex rest api for accessing the account using the account id. Access token would be needed for authorising the request.

  • Request Type  GET
  • url – <org address>/ services/apexrest/Accounts/<AccountId>
  • Header
    • Authorization  Bearer <access token>

If you want to send some parameters along with the request (post method ) then include it in the body of the request as JSON and add Content-Type as application/JSON in the header part.

Download the request collection JSON file that I have used for this blog. If you want to make use of this, then choose the import option.

Don’t forget to share the post if you like it & bookmark the blog for future references. If you have any comments or doubts about this post, Please comment on the box.

반응형