Reputation: 1699
Good morning.
I'm currently trying to access the POST data from a curl request in a Symfony 4 Controller.
My controller looks like the following:
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',
]);
}
}
When I run php bin\console server:run
and access the localhost/order in the browser I get the page it is supposed to get. If I curl the url with
curl http://127.0.0.1:8000/order
I also get the right results. But when I try to send a json body on the curl request, on the above controller I just get an empty array.
My request looks like the following:
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
Any idea on what am I doing wrong?
Upvotes: 0
Views: 1883
Reputation: 1722
The Request object will only return form parameters via $request->request methods. if you want to access the JSON body, you need to use $request->getContent(). If that content is json, your code will look something like this:
$json = json_decode($request->getContent(), true);
var_dump($json['json']);
Upvotes: 1