Getting binary content in Node.js using request

Javascriptnode.jsRequestBinaryfilesRequestjs

Javascript Problem Overview


I was trying to GET a binary data using request, and had something like:

var requestSettings = {
    method: 'GET',
    url: url,
};
request(requestSettings, function(error, response, body) {
    // Use body as a binary Buffer
}

But body was always a few bytes different from expected. After further investigation I found out that request assumed body is string and replaced all non-unicode bytes.

I tried to add

encoding: 'binary'

to requestSettings but it didn't help.

How can I get the binary data?

Javascript Solutions


Solution 1 - Javascript

OK, after a lot of digging, I found out that requestSettings should have:

encoding: null

And then body will be of type Buffer, instead of the default, which is string.

Solution 2 - Javascript

The accepted answer didn't solve my problem. I somehow figured that gzip: true worked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGilZView Question on Stackoverflow
Solution 1 - JavascriptGilZView Answer on Stackoverflow
Solution 2 - JavascriptgismatthewView Answer on Stackoverflow