doulan9287 2013-05-25 02:02
浏览 37
已采纳

CakePHP REST Put / Post不接受数据

I am attempting to write a RESTful service using CakePHP 2.3.5. So far I've successfully created the GET functions for the resource I'm working with. I can send a GET request to example.com/areas.json or to example.com/areas/1.json and it returns the data in my database.

However, I started trying to get the edit function working. I wrote a simple edit method that simply saved the incoming data from $this->request->data. I'm using Postman to test the functionality and sending raw JSON over PUT or POST to example.com/areas/1.json returns a message telling me that the data couldn't be saved. I made the method send me more information when it failed and it tells me that there is no incoming data in either $this->request->data or $this->data.

I've been searching the Internet for solutions to this or similar problems, but everything I have tried has failed so far. I've attempted disabling CSRF checks, disabling the SecurityComponent altogether, and multiple other fixes all involving the security. Changing any of those resulted in black holing the request.

Does anyone have any thoughts on what else I could try to get CakePHP to accept the JSON data into a request? I'll include my edit function below in case that helps.

public function edit($id)
{
    $this->Area->id = $id;
    $message['request-data'] = $this->request->data;
    if ($this->Area->save($this->request->data)) {
        $message['response'] = $this->Area->findById($id);
    } else {
        $message['response'] = "Error";
    }
    $this->set(array(
        'message' => $message,
        '_serialize' => array('message')
    ));
}
  • 写回答

1条回答 默认 最新

  • dqqy64515 2013-05-25 02:47
    关注

    First, make sure the Content-Type of the request is application/json.

    Second, CakePHP doesn't automatically decode the JSON payload; you have to do it manually. From the manual:

    // Get JSON encoded data submitted to a PUT/POST action
    $data = $this->request->input('json_decode');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程