2
@RestResource(urlMapping='/Merchandise/*')
global with sharing class MerchandiseManager {

    @HttpGet
    global static Merchandise__c getMerchandiseById() {
        RestRequest req = RestContext.request;        
        String merchId = req.requestURI.substring(
                                  req.requestURI.lastIndexOf('/')+1);
        Merchandise__c result = 
                       [SELECT Name,Description__c,Price__c,Total_Inventory__c
                        FROM Merchandise__c 
                        WHERE Id = :merchId];
        return result;
    }

    @HttpPost
    global static String createMerchandise(String name,
        String description, Decimal price, Double inventory) {
        Merchandise__c m = new Merchandise__c(
            Name=name,
            Description__c=description,
            Price__c=price,
            Total_Inventory__c=inventory);
        insert m;
        return m.Id;
    }
}

i am trying to execute these from postman,

https://na8.salesforce.com/services/apexrest/MerchandiseManager?name=GenePoint

but throwing below error,how i can authenticate,please help urgent requirement.

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

2 Answers 2

8

You can't connect Salesforce without authentication so you need to first generate app key and secret and then you will use that to call your api.

You can follow below steps:

  1. Create Connected app in salesforce from Setup->Create-> Apps

enter image description here

  1. After successful completion of previous step you will get consumer key and consumer secret as shown below.

enter image description here

  1. Open postman and input setup as shown below

enter image description here

  1. Copy access token and instance url for next step.

enter image description here

  1. Connect to your rest api endpoint (https://na8.salesforce.com/services/apexrest/MerchandiseManager?name=GenePoint)

enter image description here

Ref document : http://sfdcbeginner.com/how-to-test-salesforce-rest-api.html

14
  • Hi HImansu,i cant see form over there,developer edition can make use of postaman/!
    – Satya
    Sep 12, 2016 at 7:15
  • yes you can use developer edition.
    – Himanshu
    Sep 12, 2016 at 7:33
  • i had added Advanced REST Api,i entered url like login.salesforce.com/services/oauth2/token,,i can see form over there
    – Satya
    Sep 12, 2016 at 7:36
  • what is the error you are getting
    – Himanshu
    Sep 12, 2016 at 7:36
  • 400 bad request
    – Satya
    Sep 12, 2016 at 7:40
0

I recently wrote an article exactly on how to connect to Salesforce using Postman.

As @Himanshu mentioned, you need to define a connected app to authenticate to.

I hope you find the article useful: http://blog.mkorman.uk/using-postman-to-explore-salesforce-restful-web-services/

7
  • where can i see client id
    – Satya
    Sep 12, 2016 at 10:08
  • it's defined in your connected app in Salesforce.
    – mkorman
    Sep 12, 2016 at 10:11
  • 1
    is it consumer key
    – Satya
    Sep 12, 2016 at 10:12
  • what is Auth URL and Access Token URL,,i did not see any urls with this name.
    – Satya
    Sep 12, 2016 at 10:14
  • Those are well-known URL's, and they are mentioned in the post above: https://login.salesforce.com/services/oauth2/authorize and https://login.salesforce.com/services/oauth2/token
    – mkorman
    Sep 12, 2016 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .