I have a request that is supposed to download a file from remote api. What I want is, however, to save this file with the same name which it is saved with when I download the file from browser. For example, I have an URL https://myapi.com/files/4hjiguo4ho45946794526975429
, and when I click this link, browser immediately starts to download a file from that URL with name myfile20180601.txt
. How do I save the file with the same name if I make a request from Node.js? This is my code:
axios({
method: 'get',
url: 'https://myapi.com/files/4hjiguo4ho45946794526975429',
responseType: 'stream',
headers: {
Authorization: 'Basic KJVEB46287blablablatoken'
}
})
.then(res => res.data.pipe(fs.createWriteStream(`${/* filename */}.txt`)))
.catch(err => console.error(err));