Created
August 11, 2018 05:47
-
-
Save aniket91/2f6e92a005eb2a62fcc1ddd39aac6dc2 to your computer and use it in GitHub Desktop.
A sample nodejs code to make https call using standard https module
This file contains 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
/** | |
* 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where will I place this code in my static web page to make it live