I'm sending a POST request in Node.js to a php server. In the request i include the json as an object and not as a string.
Node.js request:
var request = require('request');
let data = { "name":"John", "age":30, "car":null };
request.post({
headers: {'content-type' : 'application/json; charset=utf-8'},
url: 'http://0.0.0.0:9000/html.php',
method: 'POST',
json: data
}, function(error, response, body){
console.log(body);
});
html.php:
<?php
# Get JSON as an object
$json = file_get_contents('php://input');
$name = $json->name;
echo $json // Prints out the whole json correctly
echo $name; // Prints out "undefined"
I expect the output to be "John" instead of "undefined"