I have a POST endpoint that receives a JSON payload.

I'm using the Magento\Framework\App\Request\Http class to try and retrieve my POST data.

If I run $data = $this->request->getPost();, then $data will be empty.

However if I run $data = file_get_contents('php://input'); I am able to view my JSON payload as a string.

Is there a way to grab this natively through Magento?

FWIW I'm testing this through a rest/V1 endpoint through Postman.
This is a POST request with a chunk of (valid) JSON data in the raw data type.

  • try $this->request->getContent(); – fmsthird Mar 19 '19 at 0:50

Assuming $this->request is an instance of Magento\Framework\App\Request\Http, you can use the code below to get the JSON post body.

$json = $this->request->getContent();
$data = json_decode($json);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.