How can one POST JSON to the WebServer Module via Node's Request Module?
I have a phantomjs webserver setup to listen to JSON POSTs
var server = require('webserver').create();
var service = server.listen("127.0.0.1:3001", function(request, response) {
console.log(JSON.stringify(request.post));
response.setHeader("Content-Type", 'application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.write(JSON.stringify({ result: 'done'' }));
response.statusCode = 200;
response.close();
});
I have a node app that's trying to make a POST to the webserver. However, when that request gets to the server, `request.post` shows "undefined".
var request = require('request');
request.post('http://localhost:3001', {
headers: {
"Accept":"*/*",
"Accept-Encoding":"gzip,deflate,sdch",
"Accept-Language":"en-US,en;q=0.8",
"Cache-Control":"max-age=0",
"Connection":"keep-alive",
"Content-Type":"application/x-www-form-urlencoded",
"Host":"localhost:3001",
"Origin":"http://localhost:8082",
"Referer":"http://localhost:8082/",
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
}
}).form({ foo: 'foo!' });
Mysteriously, a POST made by JQuery client-side works (and is where I tried copying most of my parameters from).
$.post("http://localhost:3001", { foo: 'foo!' });
Proceed only if we were able to read the "Content-Length"
var body = JSON.stringify({ foo: 'foo!' });
headers: {
"host": "localhost:8020","Accept": "application/json","Content-Type": "application/json","Content-Length": body.length,"Connection": "keep-alive"},body: body});