Skip to content

Instantly share code, notes, and snippets.

@Unitech
Created May 12, 2014 13:47
Show Gist options
  • Save Unitech/cb39287b9f083f156bd2 to your computer and use it in GitHub Desktop.
Save Unitech/cb39287b9f083f156bd2 to your computer and use it in GitHub Desktop.
Simple POST nodejs
obj.post = function(url, data, cb) {
var http = require('http');
var dt = JSON.stringify(data);
var options = {
host: url,
path: '/new',
port: 3000,
method: 'POST',
headers : {
'Content-Length': dt.length,
'Content-Type': 'application/json'
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(dt, 'utf8');
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment