Reputation: 1221
I've this example from here
const https = require('https');
const options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET'
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
In the https.request
response how to get the body (html) of the response. I mean which property to use in d object in process.stdout.write(d);
?
Upvotes: 2
Views: 63
Reputation: 319
In the example given above, process.stdout.write(d) writes the response body to the console. If you want to use console.log(), you can use it as given bellow,
res.on('data', (d) => {
process.stdout.write(d);
console.log(d.toString('utf8'));
});
d.toString('utf8') is required as d is a buffer variable and is required to be converted to utf8 string.
d.toString('utf8')
The above converts buffer to string.
Upvotes: 2