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
Rajeesh vrRajeesh vr 

Query string is not working in test class for REST API

I have created a REST API with url like "param1/param2/?query1=value1&query2=value2". The API is working and I am getting the response from Salesfore. But when I tried to create a test class for the above API, I am not getting query values passing from the test class to the API Apex class. I have tried to debug it and I was getting null values for the queey strings passed from the test class. Any idea why test class is not supporting query string in the API URL?
pigginsbpigginsb
Hi, Rajeesh. I'm making a guess without seeing the code you are working with. I had also developed an Apex class exposed as a @RestResource, and my approach to adding URL parameters in the test class looks something like the below snippet.
// create a test request...
RestRequest req = new RestRequest();
req.requestURI = '/services/apexrest/abc/xyz/';
req.httpMethod = 'GET';

// add URL parameters here...
reg.addParameter('param1', 'value1');
reg.addParameter('param2', 'value2');
reg.addParameter('param3', 'value3');

// create a test response...
RestResponse resp = new RestResponse();

// instantiate your @RestResource Apex class...
MyApexRest myRest = new MyApexRest();

// pass the request and response to the "handle request" method to test...
myRest.handleRequest(req, resp);
Here is the documentation regarding the RestRequest (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_restrequest.htm) class.

Please let me know if this is helpful, or share your code if there is more to resolve.