smerny smerny - 1 year ago 307
JSON Question

Send POST data via raw json with postman

I've got Postman (the one that doesn't open in Chrome) and I'm trying to do a POST request using raw json.

In the Body tab I have "raw" selected and "JSON (application/json)" with this body:

"foo": "bar"

For the header I have 1,
Content-Type: application/json

On the PHP side I'm just doing
for now, and I'm getting an empty array.

If I use jQuery and do:

"type": "POST",
"url": "/rest/index.php",
"data": {
"foo": "bar"
}).done(function (d) {

I'm getting as expected:

[foo] => bar

So why isn't it working with Postman?

Postman screenshots:

enter image description here

and header:

enter image description here

Answer Source

Unlike jQuery in order to read raw JSON you will need to decode it in PHP.

print_r(json_decode(file_get_contents("php://in‌​put"), true));

php://input is a read-only stream that allows you to read raw data from the request body.

$_POST is form variables, you will need to switch to form radiobutton in postman then use:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download