Friday, August 15, 2014

Calling APEX Rest service using Postman

This post explains the setup of Postman REST Client.  Postman REST client is an add-on for chrome that can connect to Salesforce using OAUTH 2.0 password flow.  Using this method, you can get the access token to call your APEX rest service.  

This topic assumes that you already have Postman REST client.  In case you don't have Postman REST Client, you can download from Chrome Store


Setting up OAUTH

Open Postman and click on new Collection to create a new collection.  Then enter the collection name as shown in below




Click on Create to create a new collection.  This will create a new collection

Now, we are ready to get the access token from Salesforce using OAUTH.  Enter the Authentication URL to salesforce under Normal Settings of the Postman.  For Sandbox the Authentication URL is https://test.salesforce.com/services/oauth2/token and for production it is https://login.salesforce.com/services/oauth2/token.

Then select form-data is a method of POST to salesforce.  The following key value paris are necessary to post the authentication information to Salesforce.  

  •  grant_type=password
  • client_id=<clientId>
  • client_secret=<clientSecret>
  • username=<username>
  • password=<password>
The values for <clientId> and <clientSecret> need to be configured as an OAUTH application in Salesforce.  For more information on the setting up app access, refer to sale force documentation here..  Save the script.  You have the script read.  Now, add the script to the collection you have just created, by selecting "Add to Collection" button.



To get the access token, click on the "Send" button to get the send the authentication request.  If the user name, password, client id, client secret is correct, salesforce will return a JSON structure that contains access token.

You can now use this access token in the Request header to process calls in Salesforce.



  • Crete a new service by cloning the existing OAUTH service. 
  •  Change the URL to your apex service.  The format of the URL will be:  <instance_name>/services/apexrest/<servicename>/<version>  In this example we will us helloWorld Service.  so the instance url will be <instance_name>/services/apexrest/helloWorld/
  • Create a new Header Authorization parameter that has the access_token from the previous step.
  • Set the Content-Type as 'application/json'
  • Enter the JSON message in the Body to be submitted to Salesforce


The sample call to helloWorld Service in Salesforce is shown in screen capture below.  This service uses HTTP POST to post the message and the service responds back with the helloWorld in the respective language.




Now you can test your web services by click of a button.  Happy development!