Skip to content

Instantly share code, notes, and snippets.

@joe-gaudet-hs
Last active April 11, 2016 23:21
Show Gist options
  • Select an option

  • Save joe-gaudet-hs/caac7e49979a1b1ec67c3c842dc245e9 to your computer and use it in GitHub Desktop.

Select an option

Save joe-gaudet-hs/caac7e49979a1b1ec67c3c842dc245e9 to your computer and use it in GitHub Desktop.
http.request(options, response => {
let body = [];
if (response.statusCode < 200 || response.statusCode >= 400) {
reject('HTTP ' + response.statusCode);
}
response
.on('data', chunk => {
body.push(chunk);
})
.on('end', () => {
// No more data in response
try {
resolve(JSON.parse(body.join('')), response.headers);
} catch (e) {
reject(e.message);
}
})
});
})
.on('error', e => {
reject(e.message);
});
.write(body.length && body);
.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment