Skip to content

Instantly share code, notes, and snippets.

@marciopuga
Created March 26, 2019 04:45
Show Gist options
  • Save marciopuga/1b0ab6701cfc7f6a1574a92a3c634f5b to your computer and use it in GitHub Desktop.
Save marciopuga/1b0ab6701cfc7f6a1574a92a3c634f5b to your computer and use it in GitHub Desktop.
Send POST request using Vanilla Node.js
const http = require('http')
const options = {
hostname: 'localhost',
port: 3000,
path: '/path',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
var req = http.request(options, function (res) {
res.setEncoding('utf8')
res.on('data', function (body) {
console.log('Body: ' + body)
})
})
req.on('error', function (e) {
console.log('problem with request: ' + e.message)
})
const data = {hello: "world"}
req.write(JSON.stringify(o))
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment