Created
December 28, 2021 08:59
-
-
Save phatnguyenuit/95acdefbf3bbb7b3cc70f3147b6b1ad0 to your computer and use it in GitHub Desktop.
Native NodeJS request GET with https.get
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
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