Skip to content

Instantly share code, notes, and snippets.

@D1360-64RC14
Last active May 25, 2021 00:23
Show Gist options
  • Save D1360-64RC14/1d1dcf6ae970760118edc98bf8d2893a to your computer and use it in GitHub Desktop.
Save D1360-64RC14/1d1dcf6ae970760118edc98bf8d2893a to your computer and use it in GitHub Desktop.
Node.JS Simple HTTPS Request
const HTTPS = require("https");
/**
* @param {string | HTTPS.RequestOptions | URL} options
* @returns {Promise<string>}
*/
function MakeRequest(options) {
return new Promise((Ok, Err) => {
HTTPS.request(options, res => {
// console.log(`statusCode: ${res.statusCode}`);
let stack = "";
res.on("data", data => {
stack += data;
});
res.on("end", () => {
Ok(stack);
})
})
.on("error", Err).end();
})
}
// Fake user api
MakeRequest("https://reqres.in/api/users/2")
.then(console.log)
.catch(console.error);
/* Output:
{
"data":{
"id":2,
"email":"[email protected]",
"first_name":"Janet",
"last_name":"Weaver",
"avatar":"https://reqres.in/img/faces/2-image.jpg"
},
"support":{
"url":"https://reqres.in/#support-heading",
"text":"To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment