Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample nodejs code to make https call using standard https module
/**
* Node.js code to demonstrate https calls.
* @author athakur
*/
const https = require("https");
var startDemo = function () {
console.log("starting demo code");
executeHttps(function (err, data) {
if (err) {
console.log("Error in running demo code");
}
else {
console.log("Successfully ending demo code");
}
});
}
var executeHttps = function (callback) {
var options = {
hostname: "opensourceforgeeks.blogspot.com",
port: 443,
path: "/p/about-me.html",
method: 'GET',
headers: {
'Content-Type': 'text/html'
}
};
var req = https.request(options, function (res) {
console.log("Status for API call : " + res.statusCode);
console.log("Headers for API call : " + JSON.stringify(res.headers));
res.setEncoding('utf8');
var body = '';
res.on('data', function (chunk) {
body = body + chunk;
});
res.on('end', function () {
console.log("Body for API call : " + body.length);
if (res.statusCode != 200) {
console.log("API call failed with response code " + res.statusCode);
callback("API call failed with response code " + res.statusCode, null)
} else {
console.log("Got response : " + body.length);
callback(null, body);
}
});
});
req.on('error', function (e) {
console.log("problem with API call : " + e.message);
callback(e, null);
});
req.end();
}
startDemo();
@OYEHscorpy

This comment has been minimized.

Copy link

commented Aug 12, 2018

Where will I place this code in my static web page to make it live

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.