Skip to content

Instantly share code, notes, and snippets.

@Conduitry
Last active November 3, 2018 11:54
Show Gist options
  • Save Conduitry/dd2708d0414dd5e87253c6dcd2fb7229 to your computer and use it in GitHub Desktop.
Save Conduitry/dd2708d0414dd5e87253c6dcd2fb7229 to your computer and use it in GitHub Desktop.
Node.js http[s].request helper
// ClientRequest -> Promise<IncomingMessage> with an extra .body field that is a Buffer of the entire response
const response = request =>
new Promise((resolve, reject) =>
request
.once('response', response => {
const chunks = [];
response.on('data', chunk => chunks.push(chunk)).once('end', () => {
response.body = Buffer.concat(chunks);
resolve(response);
});
})
.once('error', error => reject(error)),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment