By smerny


2016-08-18 00:09:20 8 Comments

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

4 comments

@Neo 2017-03-29 05:48:44

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

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

@meda 2016-08-18 00:32:35

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

@smerny 2016-08-18 01:20:26

The problem with form in Postman is that my it doesn't deal with nested data and arrays. My real json is more complex than foo=bar.

@smerny 2016-08-18 01:39:26

I guess it appears that Postman cannot behave like jQuery's ajax and that I have to make jQuery's ajax work like Postman using these properties: 'data': JSON.stringify({foo:'bar'}), 'contentType': 'application/json' ... and then use file_get_contents() like above... then it will work with both jquery ajax and postman's raw json. Thanks

@meda 2016-08-18 02:02:50

yes exaclty posting json vs post variables are different

@Jaroslav Štreit 2018-06-28 10:17:02

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

@jhpratt 2019-01-01 07:36:22

This doesn't answer the question.

@Jaroslav Štreit 2019-01-02 08:25:27

@jhpratt actually it does. I had same problem. When used Postman app, it worked. Question is: So why isn't it working with Postman?

@CoredusK 2017-04-27 09:22:49

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!

@Charlie Stanard 2018-08-20 18:24:08

Glad I scrolled down a little further and saw this - I was having the same issue. Good find!

Related Questions

Sponsored Content

17 Answered Questions

1 Answered Questions

[SOLVED] POST Request works with Postman, but not with Guzzle

11 Answered Questions

0 Answered Questions

ATG REST service not getting raw POST JSON data from POSTMAN

2 Answered Questions

[SOLVED] how to send object using postman rest client to call REST service

12 Answered Questions

[SOLVED] JavaScript/jQuery to download file via POST with JSON data

1 Answered Questions

[SOLVED] Postman send raw json not working

2 Answered Questions

[SOLVED] Send POST data with raw json with postman Express

1 Answered Questions

[SOLVED] Json object sent by postman received with null members

  • 2016-08-21 09:17:14
  • Yonatan Nir
  • 1373 View
  • 2 Score
  • 1 Answer
  • Tags:   java json postman

2 Answered Questions

[SOLVED] How to send the json data in GET request thru browser/Postman?

Sponsored Content