早上好。
我目前正在尝试访问Symfony 4 Controller中来自curl请求的POST数据。
我的控制器如下所示:
class OrderController extends AbstractController
{
/**
* @Route("/order", name="order")
*/
public function index(Request $request)
{
var_dump($request->request->all());die;
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/OrderController.php',
]);
}
}当我运行php bin\console server:run并访问浏览器中的localhost/order时,我得到了它应该得到的页面。如果我用
curl http://127.0.0.1:8000/order我也得到了正确的结果。但是当我试图在curl请求上发送json主体时,在上面的控制器上我只得到一个空数组。
我的请求如下:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '
{'json':{
"id": "1",
"customer-id": "1",
"items": [
{
"product-id": "B102",
"quantity": "10",
"unit-price": "4.99",
"total": "49.90"
}
],
"total": "49.90"
}}' http://127.0.0.1:8000/order知道我做错什么了吗?
发布于 2018-09-15 21:14:42
Request对象将仅通过$request->request方法返回表单参数。如果您想访问JSON主体,则需要使用$request->getContent()。如果该内容是json,您的代码将如下所示:
$json = json_decode($request->getContent(), true);
var_dump($json['json']);https://stackoverflow.com/questions/52348619
复制相似问题