2

I am having a problem reading JSON content using a RestResource. I am always getting a null value. My goal is to create a webhook to receive JSON content. Here is what I did.

Created a Visualforce page with a controller which is a RestResource.

<apex:page controller="StripeRestSample" action="{!doPost}">

Here is the code for the "StripeRestSample" controller.

@RestResource(urlMapping='/StripeTest')
global class StripeRestSample 
{     
    @HttpPost  
    global static void doPost()
    {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        system.debug('======================================> Step 1: ' + req);
        system.debug('======================================> Step 2: ' + res);

        if (req != null) {
            Blob body = req.requestBody;
            String bodyString = body.toString();
        }
    }      
}

And then, exposed the Visualforce page using a site.

Now my end point is:

https://mydomain.secure.force.com/ipn/StripeTest

Then I am trying to test it with the following cURL command.

cURL -v -H "Content-Type: application/json" -d '{"test" => "test" }' https://mydomain.secure.force.com/ipn/StripeTest

Every time I run the cURL command, I could see a request coming in from the debug logs and the values are always null. Here is the debug log:

01:17:02.022 (22088027)|EXECUTION_STARTED
01:17:02.022 (22125597)|CODE_UNIT_STARTED|[EXTERNAL]|066330000004plG|VF: /apex/StripeTest
01:17:02.038 (38138779)|CODE_UNIT_STARTED|[EXTERNAL]|01p330000005c8r|StripeRestSample 
01:17:02.038 (38157097)|SYSTEM_MODE_ENTER|true
01:17:02.038 (38761812)|CODE_UNIT_FINISHED|StripeRestSample 
01:17:02.038 (38902335)|CODE_UNIT_STARTED|[EXTERNAL]|01p330000005c8r|StripeRestSample invoke(doPost)
01:17:02.039 (39211422)|USER_DEBUG|[10]|DEBUG|======================================> Step 1: null
01:17:02.039 (39282186)|USER_DEBUG|[11]|DEBUG|======================================> Step 2: null
01:17:02.039 (39324631)|CODE_UNIT_FINISHED|StripeRestSample invoke(doPost)
01:17:02.039 (39361984)|VF_APEX_CALL|j_id0|{!doPost}|PageReference: none
01:17:02.049 (49099697)|CUMULATIVE_LIMIT_USAGE

Really appreciate if you can let me know where and what I am doing wrong.

0

might be because of the data format you are passing in

-d '{"test" => "test" }' is not a valid JSON

try changing it to below and see if it works

-d "{\"test\":\"test\"}"
  • Hi Vamsi Krishna, Thanks for your suggestion. I tried changing the JSON as you suggested, but I am still getting the same result. – Prasad Jan 7 '16 at 17:07

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.