[SalesForce] Apex HTTP Post method parameters

Can a REST Http post method get a custom object( containing other custom objects and standard objects ) as a parameter if so how would the http post request body look like ? Thank you all for reading this : )

@RestResource(urlMapping='/myCustomREST/v1/*')
global with sharing class RestCustomClass {
global class myCustomRequest {
    public MyCustomObj customObj;
    public Account account;
    public Contact contact;

}

…..

Best Answer

The RestRequest documentation explains that:

If the Apex method has no parameters, then Apex REST copies the HTTP request body into the RestRequest.requestBody property.

So that allows you to take control of the deserialization into an instance of the class you have defined by using JSON.deserialize:

@HttpPost  
global static void post() {
    myCustomRequest data = (myCustomRequest) JSON.deserialize(
            RestContext.request.requestBody.toString(),
            myCustomRequest.class
            );
    // Use the data
}

The serialization/deserialization supports custom and standard objects.

Related Topic