+ Start a Discussion
wsmithwsmith 

RestResource with @HttpPost method with parameters, RestContext.request.requestBody is null

Using a RestResource with a @HttpPost method with parameters, the RestContext.request.requestBody is null compared to a methods without parameters. Is this a bug?

1) Without parameters, RestContext.request.requestBody is not null
@RestResource(urlMapping='/TestRestA/*')
global class WS_TestRestA {
    global class TestIt {
        global String Error;
        global Boolean IsSuccess;
    }

    @HttpPost
    global static void createTestIt() {
        String jsonStr = null;
        if (null != RestContext.request.requestBody) {
            jsonStr = RestContext.request.requestBody.toString();
        }
        System.debug(LoggingLevel.ERROR, 'jsonStr ===> ' + jsonStr);
    }
}

Using Workbench: REST Explorer with JSON {"testItRec" : {"Error" : "An error", "IsSuccess" : false}}
Output:
...
USER_DEBUG|[24]|ERROR|jsonStr ===> {
  "testItRec" : {
     "Error" : "An error",
     "IsSuccess" : false
  }
}
...  

2) With parameter, RestContext.request.requestBody is null
@RestResource(urlMapping='/TestRestA/*')
global class WS_TestRestA {
    global class TestIt {
        global String Error;
        global Boolean IsSuccess;
    }

    @HttpPost
    global static void createTestIt(TestIt testItRec) {
        String jsonStr = null;
        if (null != RestContext.request.requestBody) {
            jsonStr = RestContext.request.requestBody.toString();
        }
        System.debug(LoggingLevel.ERROR, 'jsonStr ===> ' + jsonStr);
    }​
}

Using Workbench Rest Explorer with JSON {"testItRec" : {"Error" : "An error", "IsSuccess" : false}}
Output:
...
USER_DEBUG|[34]|ERROR|jsonStr ===> null
... 
ShashankShashank (Salesforce Developers) 
As per this: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_restrequest.htm#apex_System_RestRequest_requestBody

"If the Apex method has no parameters, then Apex REST copies the HTTP request body into the RestRequest.requestBody property. If there are parameters, then Apex REST attempts to deserialize the data into those parameters and the data won't be deserialized into the RestRequest.requestBody property."
wsmithwsmith
Thanks
subramani Ramamurthysubramani Ramamurthy
pass the json format in below format it will works
 {
     "Error" : "An error",
     "IsSuccess" : false
  }