• David Ellis 24
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi,

I am facing an issue while testing a RestApi service using cURL. I am getting "INVALID_SESSION_ID" error everytime.

Here are the steps I followed:

1. Create a rest apex class.

@RestResource(urlMapping ='/Cases/*')
global class getCases
{
@HttpPost
    global static List<Case> fetchCase(String limits,Date startDate, Date endDate)
    {
        List<Case> lstCase;
        try
        {
           RestRequest req = RestContext.request;
           Integer intLimit = Integer.valueOf(req.params.get('limits'));
           DateTime strtDate = DateTime.valueOfGmt((req.params.get('startDate')).replaceAll('/','-'));
           DateTime enDate = DateTime.valueOfGmt((req.params.get('endDate')).replaceAll('/','-'));
           lstCase = [Select Id,OwnerId,CaseNumber from Case where createdDate>=: strtDate and createdDate<=:enDate limit :intLimit];
           return lstCase;
         }
         catch(Exception e)
         {
             system.debug('Exception'+e);
         }
           return lstCase;
      }
}
2. Create a connected app with Callback URL as "https://ap1.salesforce.com/services/oauth2/token"
3. Call the web service using cURL:
curl --form client_id=XXXXXXX.000XXXXXX --form client_secret=000999999--form grant_type=password --form username=gupta.pathikrit@domain.com --form password=*********** -k https://ap1.salesforce.com/services/oauth2/token
This call gave the access_token which I used to make a HTTP Post call:
curl https://ap1.salesforce.com/services/apexrest/Cases/ -H "Authorization: OAuth 00XXXXXPPP" -H "Content-Type:application/json" -d @C:\input.json -k

This is resulting in the following error:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

Can anyone help me here?

Regards,
Pathikrit