Slim 3 how to read post variables from body

Hello Anupam,

You are probably getting NULL when calling Request::getParsedBody because there is no content header set in the request indicating that the data posted is JSON. If you add the header “Content-Type: application/json” when making the POST request, the JSON data will be parsed.

If you have no control over the headers that are sent with the POST request, you can add the content type header yourself before calling Request::getParsedBody or call json_decode yourself, e.g.

$data = json_decode($req->getBody());
var_dump($data->username);
var_dump($data->age);

There is a thread on this issue at Parse JSON when no Content Type comes in Request header