[SalesForce] RestResource GET with multiple parameters

In Apex RestResource What is the correct format for urlMapping when you want to pass in multiple parameters in a GET request?
So I need to pass in accountId and serviceId as filters to GET.

@RestResource(urlMapping='/Revenues/accountId={acctId}&serviceId={svcId}');

OR

@RestResource(urlMapping='/Revenues/accountId/*/serviceId/*');

I am confused.
I tried to use the first one and then get the parameterss using
request.params.get('accountId'); but just get NULL.

Best Answer

HTTP GET defines data as a series of query parameters in the URI itself. For example, here’s a URI:

https://na8.salesforce.com/services/apexrest/FieldCase?companyName=GenePoint

So your code will be like

@RestResource(urlMapping='/Revenues/*)
@HttpGet
  global static List<Case> getOpenCases() {
    String accountId = RestContext.request.params.get('accountId');
    String accountId = RestContext.request.params.get('serviceId');

Creating REST APIs using Apex REST

Or you can parse the URl as well

@HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

Apex REST Basic Code Sample

Related Topic