How to Test Salesforce Apex REST APIs using Postman

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.

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 TypePOST
  • url – <org address>/ services/oauth2/token
  • params
    • grant_typepassword
    • 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 TypeGET
  • url – <org address>/ services/apexrest/Accounts/<AccountId>
  • Header
    • AuthorizationBearer <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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s