Getting binary content in Node.js using request

2020-01-23 05:08发布

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?

1条回答
The star\"
2楼-- · 2020-01-23 05:16

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.

查看更多
登录 后发表回答