How do we get the response.body when using got.stream ?
got.stream.put(options).on('response', (response) => {
console.log(response.body); // returns undefined
});
This is a Node.js basic question. Google "how to read a readable stream in Node.js"
I finally got it:
const body = [];
got.stream.put(options)
.on('end', () => {
console.log(Buffer.concat(body).toString());
})
.on('data', (chunk) => {
body.push(chunk);
});
You could use https://github.com/sindresorhus/get-stream to simplify that.
Thanks @sindresorhus it was easier than I thought.
I was just confused by the response event. :P
The response gives the raw response, and in 99% you don't need it unless you want to modify its properties.
Also you should not read directly from it: https://github.com/sindresorhus/got/issues/223#issuecomment-405076226
Most helpful comment
You could use https://github.com/sindresorhus/get-stream to simplify that.