[SalesForce] RESTful webservice callout with method parameters in Salesforce

I know when a REST callout is made the request body can be found in requestBody property of RestRequest class. However, this property contains body only when the method doesn't have any parameters. Like below:

@RestResource(urlMapping='/testing/*')
global class Testing_response{

    @HttpPost
    global static String testing(){
        RestRequest req = RestContext.request;
        System.debug('request body : '+ req.requestBody);
        }
    }

Can someone please explain how can we call method that contains parameters? like below:

 @HttpPost         
 global static String testingWithArg(String s, Integer k){
 }

I would then like to deserialize this request parameters. I know how everything works when the method doesnt have any parameters.

Best Answer

You don't have to deserialize the parameters, APEX would do it for you if you have sent in the parameters in the expected format.

If the Apex method has no parameters, Apex REST copies the HTTP request body into the RestRequest.requestBody property. If the method has parameters, then Apex REST attempts to deserialize the data into those parameters and the data won't be deserialized into the RestRequest.requestBody property.

You might want to check the examples provided at this link Apart from primitives, you can also send in sOjbects and List/Map of primitives/sObjects (of course with some considerations!)

In your case, you simply need to send in post data which looks like this

{“s”:”stringResponse”,”k”:123}
Related Topic