Skip to content

Instantly share code, notes, and snippets.

@aniket91
Created August 11, 2018 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniket91/2f6e92a005eb2a62fcc1ddd39aac6dc2 to your computer and use it in GitHub Desktop.
Save aniket91/2f6e92a005eb2a62fcc1ddd39aac6dc2 to your computer and use it in GitHub Desktop.
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
Copy link

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