Last active
December 10, 2022 16:13
-
-
Save PantheraRed/12fd677deee19c1a800d8b07c0726955 to your computer and use it in GitHub Desktop.
HTTP or HTTPS request in Node.js using built-in 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
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