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!

10 comments:

  1. Hi Kalyan,

    How can use postman for community users in salesforce?

    Please help me on this urgent.

    ReplyDelete
    Replies
    1. Did you get an answer to this. I have been trying to search on how to get the community users invoke an APEXREST service

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi Kalyan,

    Thanks for this informative post. Question though, is this for inbound REST calls?

    ReplyDelete
  4. Hi Kalyan,
    Can you explain what is client_id and client_secret what you given above?
    How do i can recognize client_id and client_secret for a particular salesforce account

    ReplyDelete
  5. How to use RAW type in Postman?

    ReplyDelete

  6. I am getting Session expired or invalid error. How to solve this problem. Thanks

    [
    {
    "message": "Session expired or invalid",
    "errorCode": "INVALID_SESSION_ID"
    }
    ]

    ReplyDelete
    Replies
    1. I am getting same error ... How this can be resolved ?

      Delete
  7. You need to use a valid token ID in the headers. Authorization : Bearer 00D1U00...

    This will be generated using https://yourInstance.salesforce.com/services/oauth2/token/

    supply these values ClientID/ClientSecret/UserName/Password/GrantType.

    ReplyDelete