Skip to content

Instantly share code, notes, and snippets.

@arnabdas
Created May 15, 2017 12:09
Show Gist options
  • Save arnabdas/c7c4d6d92b7510e85676f1c1906a299e to your computer and use it in GitHub Desktop.
Save arnabdas/c7c4d6d92b7510e85676f1c1906a299e to your computer and use it in GitHub Desktop.
Example of 'POST' with 'http' module of NodeJs
var http = require('http');
var postOptions = {
hostname: 'localhost',
port: '8090',
method: 'POST',
path: '/api'
headers: {
'Content-Type': 'application/json'
}
};
var req = http.request(postOptions, res => {
res.on('end', function (body) {
console.log('Ended');
});
res.on('error', err => {
console.log(util.inspect(err));
});
res.on('data', data => {
// process data
});
});
req.on('error', function(e) {
console.log('Problem with request: ' + e.message);
});
// write data to request body
req.write('{"message": "Hello, World!!!"}');
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment