我想从https请求中检索二进制数据.

我找到了一个使用请求方法的similar question

options = {
    hostname: urloptions.hostname,
    path: urloptions.path,
    method: 'GET',
    rejectUnauthorized: false,
    encoding: null
};

req = https.request(options, function(res) {
    var data;
    data = "";
    res.on('data', function(chunk) {
        return data += chunk;
    });
    res.on('end', function() {
        return loadFile(data);
    });
    res.on('error', function(err) {
        console.log("Error during HTTP request");
        console.log(err.message);
    });
})

编辑:将编码设置为'binary'也不起作用

推荐答案

被接受的答案对我不起作用(即,将编码设置为二进制),甚至提问的用户也提到它不起作用.

以下是对我有用的,摘自:http://chad.pantherdev.com/node-js-binary-http-streams/

http.get(url.parse('http://myserver.com:9999/package'), function(res) {
    var data = [];

    res.on('data', function(chunk) {
        data.push(chunk);
    }).on('end', function() {
        //at this point data is an array of Buffers
        //so Buffer.concat() can make us a new Buffer
        //of all of them together
        var buffer = Buffer.concat(data);
        console.log(buffer.toString('base64'));
    });
});

Edit:根据分号建议更新答案

Node.js相关问答推荐

解决TypScript Jest测试中的静态类块错误

在渲染上部署Node.js服务器时出错:MODULE_NOT_FOUND

如何从shell脚本中计算ecmascript模块?

Node-Red Tasmota 错误:连接 ECONNREFUSED 192.168.77.21:1883

Node.js中使用ffmpeg如何获取视频截图的位置?

使用更新版本仍然找到包@angular/fire但不支持原理图

ResponseError:键空间ks1不存在

无服务器部署使用无服务器组合抛出`spawn serverless ENOENT`

使用mongoose 创建新文档并仅取回选定的字段

获取用户 ID 后,Firebase 函数 onCreate 方法在 Firestore 上不起作用

nuxt:在 docker 镜像中找不到

在 Express.js 中迭代子文档数组

Mongoose,如何一次更新多个?

Node js中向rest服务发送https请求的步骤

等价于 Node.js 的 Rails 控制台

如何使用 gulp-uglify 缩小 ES6 函数?

如何监控 node.js 上的网络,类似于 chrome/firefox 开发者工具?

使用 pm2 编程 api 重命名进程

Node.js -Firebase 服务帐户私钥不会解析

Node.js 中的 PHP exit()/die() 类似功能是什么