0

I want to know how Apex parse rest post data.. Like in the following example:

// Apex code for Post service
@HttpPost
global static String doPost(String n,String site, String name) {
    Account account = new Account();
    account.AccountNumber = n;
    account.Site = site;
    account.Name = name;
    insert account;
    return account.Id;
}

// Post request data body
{
"site" : "www.hubbly.com",
"name" : "div",
"n" : "234"
}

Does it use HashMap internally as it requires parameter name and body key to be same?Link for source code will be helpful... Thanks!

0

If you take a look at the documentation, you will find the details around this. It just works as any standard Http contract implementation and not necessarily on map concepts.

Apex REST supports two formats for representations of resources: JSON and XML. JSON representations are passed by default in the body of a request or response, and the format is indicated by the Content-Type property in the HTTP header. You can retrieve the body as a Blob from the HttpRequest object if there are no parameters to the Apex method. If parameters are defined in the Apex method, an attempt is made to deserialize the request body into those parameters. If the Apex method has a non-void return type, the resource representation is serialized into the response body.

It's the request header content type which determines if the body is to be deserialized in a specific format or not, and not the request body contents itself.

If you accept parameters in your POST method, that's when the name of the parameters matter. Refer to the below excerpt from the same documentation link:

The names of the Apex parameters matter, although the order doesn’t.

1

There's no "source code" for this; this is a feature of using parameters in an HttpPost method. It automatically parses the JSON or XML payload, deserializes the data into native objects (e.g. numbers, Boolean, etc). There are methods for explicit parsing using RestResource.request, but these are suppressed if you use parameters (e.g. the body won't contain the raw JSON). If you use the parameterized version of a function, the names of the parameters must match the XML or JSON keys used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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