使用JSON输入CakePHP的API PUT [英] CakePHP API PUT with JSON input

查看:112
本文介绍了使用JSON输入CakePHP的API PUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP构建一个API。

I am building an API using CakePHP.

我想用从我的移动应用PUT更新数据。格式是JSON作为输入,但$这个 - >数据似乎为空。

I want to use PUT from my mobile application to update data. The format is JSON as input but $this->data seems to be null.

我把这个网址(如在文档中指定)从我的应用程序:
/recipes/123.json

I call this url (as specified in the docs) from my application: /recipes/123.json

在我的配方(或其他),我有以下控制器:

And in my "recipes" (or whatever) I have the following controller:

function edit($id = null) {

    $this->User->id = $id;
    if (empty($this->data)) {
        $this->data = $this->User->read();
        $message = array('StatusCode' => 999, 'ERROR' => "");
    } else {
        if ($this->User->save($this->data)) {
            $message = array('StatusCode' => 200, 'ErrorCode' => "");
        } else {
            $message = array('StatusCode' => 400, 'ErrorCode' => "UnknownError");
        }

    }

    $this->set(compact("message"));

    $this->set('albums', $this->User->Album->find('list'));
}

我正确地接收我的应用程序JSON响应但是我得到的999错误 - 这意味着$这个 - >数据是空的。
在我的控制器,其中接收JSON使用POST我的附加功能 - 在$这个 - >数据被正确分配。哦你们,如果我用,而不是我的编辑PUT POST - $的这 - >数据获取设置,但我不能保存数据。

I correctly receive the JSON response in my application however I get the 999 error - meaning that $this->data is empty. In my add function in my controller where it receives JSON using POST - the $this->data gets assigned correctly. And oh ye, if I use POST instead of PUT in my edit - the $this->data gets set, but I cannot save the data..

所以..我该怎么做呢? :•

So.. how do I do this ? :S

推荐答案

这luchomolina的链接插入
<一href=\"http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-xml-or-json-data\">http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-xml-or-json-data

Insert from luchomolina's link http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-xml-or-json-data

//Get JSON encoded data submitted to a PUT/POST action
$data = $this->request->input('json_decode');

,你会得到你的对象。

and you get your object.

$data->Model->field ...

这篇关于使用JSON输入CakePHP的API PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆