I defined rest API method as follows -
@RestResource(urlMapping='/verifyaccount/v1/*')
global class VerifyAccountService
{
@HttpPost
global static String VerifyAccount()
{
return 'VerifyAccountResult ==> ' + RestContext.request.params.get('AccountName');
}
}
and I am trying test it as follows - I know there is another way of testing this but I want to call it from another salesforce org and thats why I need to test it like this. and I did not specify any parameter to method itself because I wanted this method to be flexible and not bound to parameters defined in this method only.
Salesforce documentation says -
'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.' Source
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
request.setHeader('Content-Type', 'application/json');
request.setEndPoint('https://cs9.salesforce.com/services/apexrest/verifyaccount/v1/');
request.setMethod('POST');
String strbody = '{ "AccountName" : "Test Company", "Id" : "1234566" }';
request.setBody(strbody);
HttpResponse response = httpProtocol.send(request);
String jsonInput = response.getBody();
system.debug('===>'+jsonInput);
Output is "VerifyAccountResult ==> null "