+ Start a Discussion
bluecapbluecap 

Custom Rest Service - INVALID SESSION ID

Ive created a single custom Rest service that Im trying to use to return multiple sObject types with. Im using cURL to verify that the service is working. The issue Im running into is occuring when I pass more than one parameter to the service.

This works:
curl https://cs1.my.salesforce.com/services/apexrest/myService?type=a -H 'Authorization: Bearer access token'

This does not work:
curl https://cs1.my.salesforce.com/services/apexrest/myService?type=b&id=001 -H 'Authorization: Bearer access token'

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

Why would the second parameter cause this type of error?


 
Best Answer chosen by bluecap
pconpcon
I think your problem might be that you need to wrap your URL in quotes or else the terminal will treat & as backgrounding the process
 
curl "https://cs1.my.salesforce.com/services/apexrest/myService?type=b&id=001" -H 'Authorization: Bearer access token'

 

All Answers

pconpcon
Can you include the definition of your REST service?

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
bluecapbluecap
@RestResource(urlMapping='/myService')
global with sharing class RESTmyService {

    @HttpGet 
    global static ResponseHandler getData(){
        
        ResponseHandler response = new ResponseHandler();
        
        String accountid = RestContext.request.params.get('accountid');

        String type = RestContext.request.params.get('type');

         Set<String> typeSet = new Set<String>{'a','b'};
        //Types that must be passed in with an Account
        Set<String> byAccountTypeSet = new Set<String>{'b'};

        if(!isNotNullOrEmpty(type) || !typeSet.contains(type)){

            response.Status = 'Error';
            response.ErrorCode = null;
            response.Message = 'The type of request must be specified.';


        } else if(isNotNullOrEmpty(type) && byAccountTypeSet.contains(type) && !isNotNullOrEmpty(accountid)){

            response.Status = 'Error';
            response.ErrorCode = null;
            response.Message = 'Account information is required for this operation.';


        } else if(isNotNullOrEmpty(type) && (type=='a' || (byAccountTypeSet.contains(type) && isNotNullOrEmpty(accountid)))) { 
            
           type = String.escapeSingleQuotes(type);

            if(accountid!=null){
                accountid = String.escapeSingleQuotes(accountid);
            }

            List<sObject> dataCollection = new List<sObject>();
            if(type =='al'){
                //Account List
                dataCollection = [Select Id, Name from Account where RecordType.Name = 'Company' LIMIT 50000];  
            } else if(type=='fal'){
                //Contact List
                dataCollection = [Select Id, Name from Contact where AccountId =: accountid LIMIT 500]; 
            }

            if(dataCollection == null || dataCollection.size() == 0){

                response.Status = 'Error';
                response.ErrorCode = null;
                response.Message = 'No records found.';

            } else {

                response.Status = 'Success';
                response.ErrorCode = null;
                response.Message = null;
                response.Data = dataCollection;

            }
            
        }
        return response;

    }

    global class ResponseHandler {

        public String Status {get; set;}
        public List<sObject> Data {get;set;}
        public String Message {get;set;}
        public String ErrorCode {get; set;}
        
    }

    global static boolean isNotNullOrEmpty(string str)
    {
        return str!=null || !String.isBlank(str); 
    }

}
Here's the code Im using, minus most of the specifics.
pconpcon
I think your problem might be that you need to wrap your URL in quotes or else the terminal will treat & as backgrounding the process
 
curl "https://cs1.my.salesforce.com/services/apexrest/myService?type=b&id=001" -H 'Authorization: Bearer access token'

 
This was selected as the best answer
bluecapbluecap
That did it. Jeez. Thank you!