I would like to retrieve binary data from an https request.
I found a similar question that uses the request method, Getting binary content in Node
setEncoding()
method, because by default, no encoding is assigned and stream data will be returned as Buffer objectsBuffer.from()
in on.data
callback method to convert the chunk
value to a Buffer
object.http.get('my_url', (response) => {
const chunks = [];
response.on('data', chunk => chunks.push(Buffer.from(chunk))) // Converte `chunk` to a `Buffer` object.
.on('end', () => {
const buffer = Buffer.concat(chunks);
console.log(buffer.toString('base64'));
});
});