我目前正在Cakephp 3中构建RESTful API。我正在尝试通过Postman chrome扩展中的表单数据上传文件(带有一些额外的数据)。但是,我不确定如何获取通过请求收到的数据。
这是我尝试使用以下代码发送到api文件夹中的图片控制器的数据:
[MyLabelenumerateChildNodesWithName:star01 usingBlock:^(SKNode *node, BOOL *stop)
{
if ([node.name isEqualToString:star01])
{
NSLog(@"FOUND STAR");
}
}];
这会抛出一个数据库错误,说明我违反了外键约束(我认为这是因为 public function add(){
debug($this->request->data);
$picture = $this->Pictures->newEntity($this->request->data);
if ($this->Pictures->save($picture)) {
$message = 'Saved';
} else {
$message = 'Error';
}
$this->set([
'message' => $message,
'picture' => $picture,
'_serialize' => ['message', 'picture']
]);
}
为空)。但是,我只想查看发布数据的外观,但当我尝试使用$this->request->data
函数输出$this->request->data
时,debug()
不显示。我试图发布一个实际上有效的JSON主体。我的问题是如何获取通过表单数据发送的控制器中的数据。
修改
我确实设法输出了确实为空的请求数据,但$this->request-is('post')
为真。当我尝试使用$this->request
输出debug()
时,我会收到以下信息:
object(Cake\Network\Request) {
params => [
'plugin' => null,
'controller' => 'Pictures',
'action' => 'add',
'_ext' => null,
'pass' => [],
'prefix' => 'api',
'isAjax' => false
]
data => null
query => []
cookies => []
url => 'api/pictures/add'
base => ''
webroot => '/'
here => '/api/pictures/add'
trustProxy => false
[protected] _environment => [
'USER' => 'www-data',
'HOME' => '/var/www',
'FCGI_ROLE' => 'RESPONDER',
'QUERY_STRING' => '',
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => 'multipart/form-data',
'CONTENT_LENGTH' => '375',
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/api/pictures/add',
'DOCUMENT_URI' => '/index.php',
'DOCUMENT_ROOT' => '/home/vagrant/Apps/vecto.app/webroot',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'GATEWAY_INTERFACE' => 'CGI/1.1',
'SERVER_SOFTWARE' => 'nginx/1.7.9',
'REMOTE_ADDR' => '10.33.10.1',
'REMOTE_PORT' => '55842',
'SERVER_ADDR' => '10.33.10.10',
'SERVER_PORT' => '80',
'SERVER_NAME' => 'vecto.app',
'REDIRECT_STATUS' => '200',
'SCRIPT_FILENAME' => '/home/vagrant/Apps/vecto.app/webroot/index.php',
'HTTP_HOST' => 'vecto.app',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_CONTENT_LENGTH' => '375',
'HTTP_ACCEPT' => 'multipart/form-data',
'HTTP_POSTMAN_TOKEN' => 'be1a629c-d81e-b19b-4e0f-294a46779150',
'HTTP_CACHE_CONTROL' => 'no-cache',
'HTTP_ORIGIN' => 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
'HTTP_AUTHORIZATION' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
'HTTP_CONTENT_TYPE' => 'multipart/form-data',
'HTTP_DNT' => '1',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8,nl;q=0.6',
'PHP_SELF' => '/index.php',
'REQUEST_TIME_FLOAT' => (float) 1457090949.1208,
'REQUEST_TIME' => (int) 1457090949,
'HTTP_X_HTTP_METHOD_OVERRIDE' => null,
'ORIGINAL_REQUEST_METHOD' => 'POST',
'HTTPS' => false,
'HTTP_X_REQUESTED_WITH' => null
]
[protected] _detectors => [
'get' => [
'env' => 'REQUEST_METHOD',
'value' => 'GET'
],
'post' => [
'env' => 'REQUEST_METHOD',
'value' => 'POST'
],
'put' => [
'env' => 'REQUEST_METHOD',
'value' => 'PUT'
],
'patch' => [
'env' => 'REQUEST_METHOD',
'value' => 'PATCH'
],
'delete' => [
'env' => 'REQUEST_METHOD',
'value' => 'DELETE'
],
'head' => [
'env' => 'REQUEST_METHOD',
'value' => 'HEAD'
],
'options' => [
'env' => 'REQUEST_METHOD',
'value' => 'OPTIONS'
],
'ssl' => [
'env' => 'HTTPS',
'options' => [
(int) 0 => (int) 1,
(int) 1 => 'on'
]
],
'ajax' => [
'env' => 'HTTP_X_REQUESTED_WITH',
'value' => 'XMLHttpRequest'
],
'flash' => [
'env' => 'HTTP_USER_AGENT',
'pattern' => '/^(Shockwave|Adobe) Flash/'
],
'requested' => [
'param' => 'requested',
'value' => (int) 1
],
'json' => object(Closure) {
},
'xml' => object(Closure) {
},
'mobile' => object(Closure) {
},
'tablet' => object(Closure) {
},
'api' => object(Closure) {
}
]
[protected] _detectorCache => [
'json' => false,
'xml' => false,
'api' => false,
'post' => true,
'ajax' => false
]
[protected] _input => '------WebKitFormBoundaryRDGaZEhAuN4lILOR
Content-Disposition: form-data; name="album_id"
2
------WebKitFormBoundaryRDGaZEhAuN4lILOR
Content-Disposition: form-data; name="description"
haiahaahahahdaisdhisadhisadihsdhiiahsd
------WebKitFormBoundaryRDGaZEhAuN4lILOR
Content-Disposition: form-data; name="favorite"
true
------WebKitFormBoundaryRDGaZEhAuN4lILOR--
'
[protected] _session => object(Cake\Network\Session) {
[protected] _engine => null
[protected] _started => null
[protected] _lifetime => '1440'
[protected] _isCLI => false
}
}
有谁知道我在这里做错了什么?看起来我正在接收文本值,但不是同时发送的文件。除此之外,我还是不知道如何获取album_id和description等数据。
编辑2 当我使用debug()输出$ this-> request-> input()(不带json_decode)时,我得到以下输出:
'------WebKitFormBoundarykeFhH2KM4LdYgsH0
Content-Disposition: form-data; name="album_id"
2
------WebKitFormBoundarykeFhH2KM4LdYgsH0
Content-Disposition: form-data; name="description"
haiahaahahahdaisdhisadhisadihsdhiiahsd
------WebKitFormBoundarykeFhH2KM4LdYgsH0
Content-Disposition: form-data; name="favorite"
true
------WebKitFormBoundarykeFhH2KM4LdYgsH0--
'
我现在的问题是如何获取同样通过同一请求发送的文件? (最后一个边界似乎是空的)
答案 0 :(得分:0)
cake没有设置$this->request->data
的原因是因为POST请求主体是以JSON格式编码的。
According to the CakePHP documentation:
使用REST的应用程序经常在非URL编码的帖子体中交换数据。您可以使用Network \ Request :: input()以任何格式读取输入数据。通过提供解码功能,您可以以反序列化的格式接收内容:
// Get JSON encoded data submitted to a PUT/POST action
$data = $this->request->input('json_decode');
$data
现在应该包含Postman提交的信息。
答案 1 :(得分:0)
我终于通过调用debug($ this-> request-> input())(没有json_decode)找到了解决方案。问题是,当文件非常大时,邮差会冻结(首先我尝试上传pdf文档)。邮差不适合输出整个文件。我有以下标题和正文:
set JAVA_COMPILER="C:\Program Files\Java\jdk1.7.0_21\bin\javac"
set CLASSPATH=.;servlet-api.jar
%JAVA_COMPILER% -deprecation -cp %CLASSPATH% -d . org\redfire\screen\*.java
pause
当我尝试调用debug($ this-> request-> input())时,这给出了以下输出:
public function add(){
debug($this->request->input());
}