1

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?

user2161301
  • 674
  • 9
  • 22

1 Answers1

0

I tried base64 printing both ways but that didn't work either since python and node seem to have different understandings of how to represent the data (what's the point of base64 then??)

After switching from got to request I noticed the same result and inspecting the returned size of the response I noticed a small size disparity between what python has received and what node got/request received. Now I had some more info to what to search for and among all the upvoted responses, this one with 0 upvotes is what has solved it for me: https://stackoverflow.com/a/59014774/2161301

Adding encoding: null as an option brings the correct size and lets me save the file in the intended way.

I've previously found a related github issue, idk if it's also applicable https://github.com/request/request-promise/issues/171

user2161301
  • 674
  • 9
  • 22