157

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

Smern
  • 18,746
  • 21
  • 72
  • 90
  • Try $_REQUEST instead $_POST and var_dump() instead print_r() – Deep Aug 18 '16 at 00:11
  • @Deep I get `array(1) {["foo"]=> string(3) "bar"}` with jQuery and still an empty array with Postman: `array(0) {}` – Smern Aug 18 '16 at 00:15
  • So Postman not sending request – Deep Aug 18 '16 at 00:18
  • @smerny in your post `fields` and `foo` wont match thus it won't work, Im not sure if it is a typo but make sure they match – meda Aug 18 '16 at 00:21
  • @meda, right that was a typo, but if I had fields it still should have output in the var dump of $_REQUEST or $_POST – Smern Aug 18 '16 at 00:22
  • @smerny so how can we troubleshoot it, you got screenshot – meda Aug 18 '16 at 00:36
  • @meda - screenshot posted – Smern Aug 18 '16 at 00:41
  • @smerny try `print_r(file_get_contents("php://input"));` because you are sending raw json. `print_r(json_decode(file_get_contents("php://input"), true));` – meda Aug 18 '16 at 00:55
  • @meda, interesting... this is giving me `{ "foo": "bar"}` with Postman now... and `foo=bar` with jQuery. I really need it to give me the same thing in both places... Postman is really just for testing – Smern Aug 18 '16 at 01:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121215/discussion-between-meda-and-smerny). – meda Aug 18 '16 at 01:05

6 Answers6

93

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

skill synon pass json to postman

Itachi
  • 2,817
  • 27
  • 35
  • 8
    this is a fairly old post.. but if you look at the question you'll see that I did exactly that. you may want to read the question, the selected answer, and the comments on the selected answer to understand the problem and solution better. – Smern Jan 29 '19 at 07:16
  • 3
    Sure, but this actually pinpointed the issue I had; I had "Text" selected, not application/json, even though that was already mentioned in the Headers section. Its confusing; if you choose "Text", then the entire HTTP POST "code" is displayed, including the JSON, which I expected to work, since its a full POST call, not just the json. – Ted Aug 18 '19 at 08:17
  • Great! this was my "issue". I've expected that the JSON is a logical default – Sinisa Rudan Mar 15 '22 at 11:01
54

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'
});
ahnbizcad
  • 10,491
  • 9
  • 59
  • 85
meda
  • 45,103
  • 14
  • 92
  • 122
8

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

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

finefoot
  • 9,914
  • 7
  • 59
  • 102
Neo
  • 523
  • 7
  • 15
8

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!

CoredusK
  • 1,173
  • 9
  • 14
  • 1
    Glad I scrolled down a little further and saw this - I was having the same issue. Good find! – Charlie Stanard Aug 20 '18 at 18:24
  • 4
    It looks like someone fixed it in an edit to meda's post. But I looked at the edit history and found that the original post had `200c 200b` (zero width characters) between the "n" and "p" in "input". Not sure how he ended up with that, but it's fixed now. – Smern Feb 14 '19 at 16:19
  • Glad I scrolled down. after around 3 hours of wastage got it fixed – Ritobroto Mukherjee Apr 16 '21 at 18:40
4

Solution 1 You can send using form-data

Solution 2 You can send using raw json data

Both solutions are working perfectly.

Thanks

Shojib Flamon
  • 1,237
  • 3
  • 12
  • 18
0

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

Jaroslav Štreit
  • 415
  • 1
  • 5
  • 16