Send POST data via raw JSON with Postman

PhpJsonRestPostman

Php Problem Overview


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 print_r($_POST); for now, and I'm getting an empty array.


If I use jQuery and do:

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

I'm getting as expected:

Array
(
    [foo] => bar
)

So why isn't it working with Postman?


Postman screenshots:

enter image description here

and header:

enter image description here

Php Solutions


Solution 1 - Php

Just check JSON option from the drop down next to binary; when you click raw. This should do

skill synon pass json to postman

Solution 2 - Php

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

print_r(json_decode(file_get_contents("php://input"), 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:

foo=bar&foo2=bar2

To post raw json with jquery:

$.ajax({
    "url": "/rest/index.php",
    'data': JSON.stringify({foo:'bar'}),
    'type': 'POST',
    'contentType': 'application/json'
});

Solution 3 - Php

I was facing the same problem, following code worked for me:

$params = (array) json_decode(file_get_contents('php://input'), TRUE);
print_r($params);

Solution 4 - Php

meda's answer is completely legit, but when I copied the code I got an error!

Somewhere in the "php://input" there's an invalid character (maybe one of the quotes?).

When I typed the "php://input" code manually, it worked. Took me a while to figure out!

Solution 5 - Php

Solution 1 You can send using form-data

Solution 2 You can send using raw json data

Both solutions are working perfectly.

Thanks

Solution 6 - Php

Install Postman native app, Chrome extension has been deprecated. (Mine was opening in own window but still ran as Chrome app)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSmernView Question on Stackoverflow
Solution 1 - PhpItachiView Answer on Stackoverflow
Solution 2 - PhpmedaView Answer on Stackoverflow
Solution 3 - PhpNeoView Answer on Stackoverflow
Solution 4 - PhpCoredusKView Answer on Stackoverflow
Solution 5 - PhpShojib FlamonView Answer on Stackoverflow
Solution 6 - PhpJaroslav ŠtreitView Answer on Stackoverflow