Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Created December 28, 2021 08:59
Show Gist options
  • Save phatnguyenuit/95acdefbf3bbb7b3cc70f3147b6b1ad0 to your computer and use it in GitHub Desktop.
Save phatnguyenuit/95acdefbf3bbb7b3cc70f3147b6b1ad0 to your computer and use it in GitHub Desktop.
Native NodeJS request GET with https.get
const https = require('https');
function getJSONRequest(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let data = '';
// called when a data chunk is received.
res.on('data', (chunk) => {
data += chunk;
});
// called when the complete response is received.
res.on('end', () => {
resolve(JSON.parse(data));
});
})
.on('error', (err) => {
reject(err.message);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment