Closed ThePedroo closed 5 months ago
check this example
const https = require('https');
const zlib = require('zlib');
const options = {
hostname: 'example.com',
path: '/path/to/resource',
method: 'GET',
headers: {
'Accept-Encoding': 'br' // Request Brotli encoding
}
};
const req = https.request(options, (res) => {
let chunks = [];
// Handle the response data
res.on('data', (chunk) => {
chunks.push(chunk);
});
// Handle the end of the response
res.on('end', () => {
const body = Buffer.concat(chunks);
// Check if Brotli encoding is used
if (res.headers['content-encoding'] === 'br') {
// Decompress the Brotli-encoded response
zlib.brotliDecompress(body, (err, decompressed) => {
if (err) {
console.error('Error decompressing Brotli response:', err);
} else {
console.log('Decompressed response:', decompressed.toString());
}
});
} else {
// Response is not Brotli-encoded, handle as needed
console.log('Response:', body.toString());
}
});
});
// Handle errors
req.on('error', (e) => {
console.error(`Error making the request: ${e.message}`);
});
// Send the request
req.end();
Thanks before anything. Yes, I'm aware of how it works with http(s) libs, the issue is that when I try using it with tls/net, which is my goal, it doesn't work
Details
Hello,
I'm making a http(s) request function using tls/net. It works fine without "Accept-Encoding" header, but when it has, and server responds with brotlii (I haven't tried others yet), it throws this error:
Node.js version
v21.1.0
Example code
Operating system
Ubuntu 23.04
Scope
code
Module and version
Not applicable.