Getting binary content in node.js with http.request

后端 未结 7 1465
广开言路
广开言路 2020-12-02 16:50

I would like to retrieve binary data from an https request.

I found a similar question that uses the request method, Getting binary content in Node

7条回答
  •  一整个雨季
    2020-12-02 17:25

    You need to set encoding to response, not request:

    req = https.request(options, function(res) {
        res.setEncoding('binary');
    
        var data = [ ];
    
        res.on('data', function(chunk) {
            data.push(chunk);
        });
        res.on('end', function() {
            var binary = Buffer.concat(data);
            // binary is your data
        });
        res.on('error', function(err) {
            console.log("Error during HTTP request");
            console.log(err.message);
        });
    });
    

    Here is useful answer: Writing image to local server

提交回复
热议问题