Created
April 2, 2016 04:31
-
-
Save SamuelMarks/9ab0052cb7a3845a7a527ce9a6763e61 to your computer and use it in GitHub Desktop.
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'); | |
function httpPOST(options, body, cb) { | |
const req = http.request(options, function (res) { | |
if (!res) | |
return cb(res); | |
else if ((res.statusCode / 100 | 0) > 3) | |
return cb(res); | |
return cb(null, res); | |
}); | |
req.write(body); | |
req.end(); | |
} | |
const body = JSON.stringify({hello: 'world'}), | |
opts0 = { | |
method: 'POST', | |
protocol: 'http:', | |
host: 'httpbin.org', | |
path: 'post', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(body) | |
} | |
}, | |
opts1 = { | |
method: 'POST', | |
protocol: 'http:', | |
host: 'httpbin.org', | |
path: '/post', | |
headers: opts0['headers'] | |
}; | |
httpPOST(opts0, body, function (error, result) { | |
if (error) console.error(error.statusCode, error.statusMessage); | |
else console.info(result.statusCode, result.statusMessage); | |
httpPOST(opts1, body, function (err, res) { | |
if (err) console.error(err.statusCode, err.statusMessage); | |
else console.info(res.statusCode, res.statusMessage); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment