142

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?

GilZ
  • 6,418
  • 5
  • 30
  • 40
  • **Note for newcomers**, [from GitHub](https://github.com/request/request) : "As of Feb 11th 2020, request is fully deprecated". Better look for alternatives. – Skippy le Grand Gourou Jan 11 '22 at 19:07

2 Answers2

318

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.

GilZ
  • 6,418
  • 5
  • 30
  • 40
  • 58
    What an absurd nightmare. Took me 12 hours to hunt this down. It seems that the Node Request module, by default, treats incoming data in the content of the response as UTF-8, and automatically converts any non-UTF-8 byte sequences to junk (but valid UTF-8) characters. No amount of setting 'mimetype", etc. works (not that it's supposed to for *response* data). The `encoding: null` is the only option that works. And - very poorly documented. There ought to be an obvious warning in the Node Request documentation about how to retrieve pure binary data. Thanks! – Dan Nissenbaum Jun 22 '15 at 18:12
  • 3
    @StoyanBerov, I'm glad you found this answer helpful, but in the 5 years since I wrote this answer, the package readme was corrected to highlight this solution in several places. In addition, I highly recommend using a package that supports Promises instead of this package. – GilZ Mar 26 '18 at 14:33
  • @Gilz, thanks for update! I was actually under the impression that encoding is set to null by default. Also, the issue came up at a legacy project, set to a super old node version and callbacks-only everywhere. – Stoyan Berov Mar 26 '18 at 17:45
1

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

gismatthew
  • 96
  • 1
  • 3