function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
seamas liseamas li 

Can not get REST API request params when testing

Hi Expert,

Why I can not get phoneNumber parameter?
Thank you so much.

I am testing Rest Api Apex code:

@RestResource(urlMapping='/mytest/*')
global with sharing class salesforceRestCall{
    
    @HttpGet
    global static String doGet() { 
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
              
        system.debug(' RestContext.request.params :: '+  RestContext.request.params);        
        
        String phoneNumber=req.params.get('subscriptionId');

        String response = processRequest( phoneNumber);

        return response;
    }


Testing Apex class:

    static testMethod void getSubscriptionByPhoneNumberTest() {
    

        test.startTest();
        
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/subscriptions?phoneNumber=416360999';
        req.httpMethod = 'GET';

        RestContext.request = req;
        RestContext.response= res;
        
        String susbscriptionDetail = salesforceRestCall.doGet();  
        test.stopTest();
    
    }
bob_buzzardbob_buzzard
Maybe because the code expects the parameter name to be 'subscriptionNumber':
 
String phoneNumber=req.params.get('subscriptionId');

 
seamas liseamas li
Hi Bob,

Thanks so much. And sorry for misunderstand.

I was using : String phoneNumber = req.params.get('phoneNumber');

And I can get phoneNumber  by Workbeck , but can not get it by Test method.