3

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.

1
  • try $this->request->getContent(); Commented Mar 19, 2019 at 0:50

1 Answer 1

4

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.