[SalesForce] Apex REST Web Service: Unexpected parameter encountered during deserialization

Any thoughts on why the REST web service below does not properly deserialize the input argument?

Unit test passes. CURL request (below) returns error.

@RestResource(urlMapping='/test/*')
global with sharing class TestService {

    @HttpPost
    global static string doPost(TestRequest req){
        return req.testArg;
    }

    private static testMethod void tests(){
        TestRequest req = new TestRequest();
        req.testArg = 'hello';
        String result = TestService.doPost(req);
        System.assertEquals('hello', result);
    }

    global class TestRequest{
        global string testArg;
    }
}

Test Request

curl -X POST -H 'Authorization: Bearer sessionID' 
-H 'Content-Type: application/json' 
-d '{"testArg": "testValue"}' 
'https://power-site-5184.database.com/services/apexrest/test'

Response

[{"message":"Unexpected parameter encountered during deserialization: testArg 
at [line:1, column:14]","errorCode":"JSON_PARSER_ERROR"}]

Update (resolved)

The correct JSON data structure should be

-d '{"req": {"testArg": "testValue"}}'

Best Answer

{
"req": {
    "testArg": "testValue"
}
}

The request data should be as shown above .

I had blogged on this topic may be that can be helpful to you

http://cloudyworlds.blogspot.in/2012/11/native-parsing-of-json-input-into-user.html

The deserialization happens with parameter name in sfdc.

Also just out of topic but its better to keep Test class in separate class as per new sfdc guidelines for test class

SFDC Documentation Link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_rest_methods.htm#ApexRESTUserDefinedTypes