[php] 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 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

This question is related to php json rest postman

The answer is


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

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


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

skill synon pass json to postman


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!


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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to rest

Access blocked by CORS policy: Response to preflight request doesn't pass access control check Returning data from Axios API Access Control Origin Header error using Axios in React Web throwing error in Chrome JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value How to send json data in POST request using C# How to enable CORS in ASP.net Core WebAPI RestClientException: Could not extract response. no suitable HttpMessageConverter found REST API - Use the "Accept: application/json" HTTP Header 'Field required a bean of type that could not be found.' error spring restful API using mongodb MultipartException: Current request is not a multipart request

Examples related to postman

Converting a POSTMAN request to Curl "Could not get any response" response when using postman with subdomain How do I format {{$timestamp}} as MM/DD/YYYY in Postman? How do I POST XML data to a webservice with Postman? How to send Basic Auth with axios How to install/start Postman native v4.10.3 on Ubuntu 16.04 LTS 64-bit? Websocket connections with Postman FromBody string parameter is giving null "Post Image data using POSTMAN" How to import Swagger APIs into Postman?