var http = require('http');
// catch data to be sent
data = {"foo":"bar"}
// parsing data
jdata = JSON.parse(data);
// create the JSON object
jsonObject = JSON.stringify(jdata);
// prepare the header
var postheaders = {
'Content-Type' : 'application/json',
'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};
// the post options
var optionspost = {
host : 'localhost',
port : 3001,
path : '/error_requests',
method : 'POST',
headers : postheaders
};
console.info('Options prepared:');
console.info(optionspost);
console.info('Do the POST call');
// do the POST call
var reqPost = http.request(optionspost, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('POST result:\n');
// process.stdout.write(d);
console.info(d);
console.info('\n\nPOST completed');
});
});
// write the json data
reqPost.write(jsonObject);
reqPost.end();
// handle error
reqPost.on('error', function(e) {
console.error(e);
});
For more details, you can have look on
Node JS Documentation
No comments:
Post a Comment