Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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 "

share|improve this question

2 Answers 2

Your example has
RestContext.request.params.get('AccountName')

I think you need to use requestBody, which is a Blob.

@HttpPost
global static String VerifyAccount()
{   
    RestRequest req = RestContext.request;
    Blob body = req.requestBody;
    String bodyString = body.toString();
    // TODO: Extract the parameter from the string
    return 'VerifyAccountResult ==> ' + bodyString );
}

You could format strBody in you example as JSON and then use JSON.deserialize or JSON.deserializeUntyped to get the values out in Apex.

share|improve this answer

Your input JSON

{
"AccountName": "Test Company",
"Id": "1234566"
}

Corresponding APEX Class for the above JSON data

public class Jsondata {  //Have as a separate or as an Inner Class

public String AccountName;
public String Id;

 }

The Modified code for your implementation

@HttpPost
global static String VerifyAccount(){   
RestRequest req = RestContext.request;
Blob body = req.requestBody;
String bodyString = body.toString();

//Deserializing the data sent into concrete SFDC objects
    Jsondata jsonwrap =(Jsondata)JSON.deserialize(bodyString,Jsondata.class);


return 'VerifyAccountResult ==> ' + jsonwrap.AccountName);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.