使用请求在 Node.js 中获取二进制内容 [英] Getting binary content in Node.js using request

查看:36
本文介绍了使用请求在 Node.js 中获取二进制内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 request GET 二进制数据,并有类似的东西:

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
}

但是 body 总是与预期的几个字节不同.经过进一步调查,我发现 request 假定 body 是字符串并替换了所有非 unicode 字节.

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.

我尝试添加

encoding: 'binary'

requestSettings 但它没有帮助.

如何获取二进制数据?

推荐答案

好吧,挖了好久才发现requestSettings应该有:

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

encoding: null

然后 body 将是 Buffer 类型,而不是默认的字符串.

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

这篇关于使用请求在 Node.js 中获取二进制内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆