kvz / cakephp-rest-plugin

Discontinued: recent cakephp versions overlap functionality, there also is @ceeram's plugin. Painless REST server Plugin for CakePHP
http://kvz.io/blog/2010/01/13/cakephp-rest-plugin-presentation/
169 stars 37 forks source link

issue with saving json data? #12

Open tito0224 opened 13 years ago

tito0224 commented 13 years ago

Hi.

Thanks for this great plug-in!! I've been messing around with it for a few hours tonight and I am very impressed.

There is one thing that I could not seem to figure out. I am trying to save a record by posting JSON data to my controller. It works fine when posting with XML data - but not JSON data.

To get the post with JSON to work I had to make a few changes and I would like to get your input as to if I am missing something.

I am using cakephp 1.3.8

I made the following modifications to get posting JSON data to save and create a record.

I had to comment out the header logic on lines 644-662

/*
    switch ($ext) {
        case 'json':
            // text/javascript
            // application/json
            if ($this->_settings['debug'] < 3) {
                header('Content-Type: text/javascript');
                $this->Controller->RequestHandler->setContent('json', 'text/javascript');
                $this->Controller->RequestHandler->respondAs('json');
            }
            break;
        case 'xml':
            if ($this->_settings['debug'] < 3) {
                header('Content-Type: text/xml');
                $this->Controller->RequestHandler->setContent('xml', 'text/xml');
                $this->Controller->RequestHandler->respondAs('xml');
            }
            break;
    }
*/

I did this in order for me to use the

$this->RequestHandler->requestedWith('json')

method in my controller's before filter to be able to decode the posted JSON and put it into $this->data:

function beforeFilter() {

    parent::beforeFilter();

    if($this->RequestHandler->isPost())
    {
        if ($this->RequestHandler->requestedWith('json')) {

            $raw_array = json_decode(trim(file_get_contents('php://input')), true);

            if (count($raw_array) == 1 && count(array_keys($raw_array)) == 1 && in_array("data", array_keys($raw_array))) {
                $this->data = $raw_array['data'];
            } else {
                $this->data = $raw_array;
            }
        }
    }
}

and because it was not being loaded in $postData in the RESTComponent it wasn't being saved to the logs table data_in column so I changed line 824 to:

'data_in' => json_encode($this->Controller->data),

Here is some sample data I am sending in:

{"data":
     {"Chart" : 
           {
                 "name": "Wages Plunged",
                 "type": "b",
                 "description": "While job losses were less severe than in recessions past, private-sector earnings declined by 10.1% between 2008 and 2009, the largests year-over-year decrease since at least 1991."
           }
     }
 }

This is working now as I expect it to with these changes, however I could not get it to work "out of the box" like the xml did. Any thoughts?

Thank you!