我目前正在尝试解析我通过Chrome中的Postman插件发送的multipart/form-data
。但是,我得到以下输出:
'------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="album_id"
2
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="description"
haiahaahahahdaisdhisadhisadihsdhiiahsd
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="favorite"
true
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="uploadfile"; filename="test2.txt"
Content-Type: text/plain
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="uploadfile"; filename="test.txt"
Content-Type: text/plain
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
------WebKitFormBoundarymsXhoqlRBbTbsZFb--
'
当我尝试调试$this->request->input()
时,我会得到此输出。当我尝试$this->request->input('json_decode')
时,我得到一个空数组,所以我假设数据不是正确的格式(只是一个字符串)。在我编写自己的算法之前,我想确保我没有用这个重新发明轮子。我做错了什么?或者如果没有,是否存在一些Cakephp 3函数来处理这个问题?
更新
我发现我的代码有什么问题,似乎路由过程以某种方式将post数组清空。当我尝试Postman时,以下端点http://vecto.app/api/pictures $ this-> request->数据为空。但是,当我尝试访问端点http://vecto.app/pictures时,$ this-> request->数据将填充信息。有谁知道以下路由设置有什么问题:
Router::prefix('api', function ($routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('Users');
$routes->resources('Pictures');
// We connect the /register action so we can simply extend the CRUD Plugin add() method
// and benefit of already available logic like validation and response codes instead of having to reinvent the wheel.
Router::connect('/api/users/register', ['controller' => 'Users', 'action' => 'add', 'prefix' => 'api']);
$routes->fallbacks('InflectedRoute');
});
答案 0 :(得分:0)
我终于找到了解决方案,它是一个清空$ this-> request->数据数组的beforeFilter。因此,输入数组不为空,但数据数组为空。这是导致问题的代码:
public function beforeFilter(Event $event) {
if ($this->request->is('post')) {
$data = $this->request;
$this->request->data = $this->request->input(function ($data) {
return json_decode($data, true);
});
}
}