Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with saving json data? #12

Open
tito0224 opened this issue May 12, 2011 · 0 comments
Open

issue with saving json data? #12

tito0224 opened this issue May 12, 2011 · 0 comments

Comments

@tito0224
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant