I make a call to a REST API and want to write the result to a file. It works in python but in node I get a faulty file that's larger in size than what I (correctly) get in python.
Python working example:
r =requests.get("""https://...""", headers=headers)
f = open("pyData.gz", "wb")
f.write(r.content)
f.close()
At first it didn't work in Python until I realized I had to write r.content
instead of r.text
since .text
is UTF-encoded (?). I also had to change the file system flag from w
to wb
. But then everything worked well.
Now to what I think is presumably the same in js:
const responseData = await got.get(`https://...`, {
headers: {
...
},
responseType: 'buffer',
throwHttpErrors: false
}).catch((err) => {
console.log('Err ', err);
});
console.log(`response length ${responseData.body.length}`) //10721
const zippedData = responseData.body;
console.log(zippedData.length) //10721
fs.open('./jsData.gz', 'w', function(err, fd) {
if (err) {
throw 'error opening file: ' + err;
}
fs.write(fd, zippedData, 0, zippedData.length, function(err, result) {
if (err) throw 'error writing file: ' + err;
fs.close(fd, function() {
console.log('file written'); //becomes 21KB in size
})
});
});
A similar issue arises to what I previously had trouble with in Python: The file is corrupt and almost twice the size of what it should be. Since I have set responseType
to buffer
and the .length is correct I think I must be doing something wrong when writing the file. I couldn't find any wb
equivalent in https://nodejs.org/api/fs.html#fs_file_system_flags - what else can I try?