To download file stream and writeFile with Node.js axios, we set the responseType
.
For instance, we write
const response = await axios({
method: "get",
url: "https://example.com/my.pdf",
responseType: "stream",
});
response.data.pipe(fs.createWriteStream("/temp/my.pdf"));
to call axios
to make a get request.
We set the responseType
to 'stream'
to download the data as a stream.
And then we call response.data.file
with a write stream to pipe the read stream we get from response
to the write stream to write the response data to the my.pdf file.