Last active
June 29, 2020 04:05
-
-
Save jcpst/19a7ce5e7d23097baae533f592775a4c to your computer and use it in GitHub Desktop.
Wrapper for node's built in http
This file contains 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
'use strict' | |
const http = require('https') | |
function request(options, body) { | |
return new Promise((resolve, reject) => { | |
const req = http.request(options, (res) => { | |
let data = '' | |
res.setEncoding('utf8') | |
res.on('data', (chunk) => (data += chunk)) | |
res.on('end', () => resolve(JSON.parse(data))) | |
}) | |
req.on('error', (e) => reject(e.message)) | |
if (body) req.write(body) | |
req.end() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment