Lumen $request->all() returns 空数组
Lumen $request->all() returns empty array
我正在尝试从 Swagger UI 向我的服务器 (Lumen) 发出 POST 请求,但每次我都得到一个空数组。
招摇UI请求:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"stocks":[ \
{ \
"contract-number":0, \
"metal-id":0, \
"amount":0, \
"unit":"string" \
} \
] \
}' 'http://backend.mywebsite.de/v1/stocks'
我的控制器:
...
public function stocksPost(Request $request)
{
dd($request->all());
}
...
并且输出:
/var/www/vhosts/localhost/htdocs/vendor/illuminate/support/Debug/Dumper.php:23:
array(0) { }
但是如果我用 Postman 发送相同的请求,一切正常:
/var/www/vhosts/localhost/htdocs/vendor/illuminate/support/Debug/Dumper.php:23:
array(1) {
'stocks' =>
string(68) "[{"contract-number": 0,"metal-id": 0,"amount": 0,"unit": "string"} ]"
}
我不明白我的问题,有人可以向我解释一下吗?
Swagger 请求的问题是它用双引号发送了我的数据,因为它不能被验证为 JSON 并且只能从 php:// 获得作为字符串输入。我使用自己的 curl 请求解决了这个问题,我要么将 JSON 数据放入单引号中,要么将其作为外部文件附加。
curl -X POST -d @foo/bar/contractsPayload.json http://backend.mywebsite.de/v1/stocks -H 'Content-Type: application/json' -H 'Accept:application/json'
我正在尝试从 Swagger UI 向我的服务器 (Lumen) 发出 POST 请求,但每次我都得到一个空数组。
招摇UI请求:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"stocks":[ \
{ \
"contract-number":0, \
"metal-id":0, \
"amount":0, \
"unit":"string" \
} \
] \
}' 'http://backend.mywebsite.de/v1/stocks'
我的控制器:
...
public function stocksPost(Request $request)
{
dd($request->all());
}
...
并且输出:
/var/www/vhosts/localhost/htdocs/vendor/illuminate/support/Debug/Dumper.php:23:
array(0) { }
但是如果我用 Postman 发送相同的请求,一切正常:
/var/www/vhosts/localhost/htdocs/vendor/illuminate/support/Debug/Dumper.php:23:
array(1) {
'stocks' =>
string(68) "[{"contract-number": 0,"metal-id": 0,"amount": 0,"unit": "string"} ]"
}
我不明白我的问题,有人可以向我解释一下吗?
Swagger 请求的问题是它用双引号发送了我的数据,因为它不能被验证为 JSON 并且只能从 php:// 获得作为字符串输入。我使用自己的 curl 请求解决了这个问题,我要么将 JSON 数据放入单引号中,要么将其作为外部文件附加。
curl -X POST -d @foo/bar/contractsPayload.json http://backend.mywebsite.de/v1/stocks -H 'Content-Type: application/json' -H 'Accept:application/json'