Read GET/POST parameters in apex class when service is called from external client

I am new to salesforce. I created an apex class and exposed it as a Rest service. I want to post some parameters to that service from an external client(not vf page). One way is I append it to the url something like myservice/functionName/parameter1/parameter2. But I want to send them as GET/POST parameters. Does anyone know how to read those parameters in the apex class?

19.8k 5 5 gold badges 57 57 silver badges 98 98 bronze badges asked Feb 11, 2016 at 18:14 31 1 1 gold badge 1 1 silver badge 2 2 bronze badges

4 Answers 4

Rather than accept say form encoded parameters it is usually more convenient (for the client and in the Apex code) to accept a JSON string that holds the parameters. For the POST case:

@HttpPost global static Result post() < String jsonString = RestContext.request.requestBody.toString(); // Use Apex JSON class to parse . >

For GET parameters you have to do your own string manipulation of the URL that you can obtain from the static field RestContext.request.requestURI . (Or better use params as Jitendra illustrates.)