Reputation: 1047
Trying to submit a JSON-Object through my site via nodeJS - Request.
var options = {
uri: 'http://localhost/test.php',
method: 'POST',
json: {
"longUrl": "http://www.google.com/"
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
});
On my test.php script, i am simply writing the $_REQUEST vars via json_encode in a debug file:
<?php
file_put_contents('test.debug.txt', "TEST: " . json_encode($_REQUEST) . json_encode($_POST));
Apparently the $_POST contains nothing. For a first debug-step, i wrote the parameters in my URL, to check if the PHP Script is working:
var options = {
uri: 'http://localhost/test.php?debug=1',
method: 'POST',
json: {
"longUrl": "http://www.google.com/"
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
});
And this additional debug=1
made it so i can see {'debug':1} as an output in my debug file. So my question right now: What happened to the POST Variables?
Output from the NodeJS Request:
0|server | request:
0|server | Request {
0|server | _events:
0|server | [Object: null prototype] {
0|server | error: [Function: bound ],
0|server | complete: [Function: bound ],
0|server | pipe: [Function],
0|server | data: [Function],
0|server | end: [Function] },
0|server | _eventsCount: 5,
0|server | _maxListeners: undefined,
0|server | method: 'POST',
0|server | body:
0|server | '{"longUrl":""http://www.google.com/"}',
Upvotes: 0
Views: 119
Reputation: 6718
If you're using the npm request
package, the proper way to post json is (doc):
options = {
uri: 'http://localhost/test.php',
method: 'POST',
body: JSON.stringify({
"longUrl": "http://www.google.com/"
}),
json: true
}
Update:
Try this to send as application/x-www-form-urlencoded
options = {
uri: 'http://localhost/test.php',
method: 'POST',
form: {
"longUrl": "http://www.google.com/"
}
}
Upvotes: 1