Created
May 15, 2017 12:09
-
-
Save arnabdas/c7c4d6d92b7510e85676f1c1906a299e to your computer and use it in GitHub Desktop.
Example of 'POST' with 'http' module of NodeJs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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