I am making a Get Request using Node-Fetch to a web api. It returns octet-stream response to be saved as a file in a local.
I tried using downloadjs (download()) and download.js (downloadBlob()), but both of them did not work.
downloadBlob() returned "createObjectURL is not a function" error, and download() returned "window is not defined at object." error.
The call is below
let res = await fetch(apiURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
})
I think I am completely lost here. What should I do to download the file to local drive? How should I construct .then block here?
downloadjs
and download.js
won't help because these are front end libraries that trigger the download process in a browser. For instance, when you generate an image on the client (in browser) and want a user to download it.
In order to save an octet-stream in Node(CLI) you can use fs
module:
const data = await fetch(apiURL, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
}).then(res => res.buffer());
fs.writeFile('filename.dat', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});