Skip to content

Instantly share code, notes, and snippets.

@PantheraRed
Last active December 10, 2022 16:13
Show Gist options
  • Save PantheraRed/12fd677deee19c1a800d8b07c0726955 to your computer and use it in GitHub Desktop.
Save PantheraRed/12fd677deee19c1a800d8b07c0726955 to your computer and use it in GitHub Desktop.
HTTP or HTTPS request in Node.js using built-in HTTPS Module.
const https = require('https');
const options = {
host: 'jsonplaceholder.typicode.com',
// port: <custom port>
path: '/users',
method: 'GET'
};
const req = https.request(options, (res) => {
let data = [];
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
const users = JSON.parse(Buffer.concat(data).toString());
console.log('\nUsers:');
for (const user of users) {
console.log(`${user.id}. ${user.name}`);
}
});
});
req.on('error', (err) => {
console.error(err);
});
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment