Getting binary content in node.js with http.request

后端 未结 7 1473
广开言路
广开言路 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:19

    Pärt Johanson I wish I could comment just to thank you for saving me from the recursive loop I've been in all day of ripping my hair out and then reading the (incredibly unhelpful) node docs on this, over, and over. Upon finding your answer, I went to dig into the docs, and I can't even find the res.setEncoding method documented anywhere! It's just shown as part of two examples, wherein they call res.setEncoding('utf8'); Where did you find this or how did you figure it out!?

    Since I don't have enough reputation to comment, I'll at least contribute something useful with my answer: Pärt Johanson's answer worked 100% for me, I just tweaked it a bit for my needs because I'm using it to download and eval a script hosted on my server (and compiled with nwjc) using nw.Window.get().evalNWBin() on NWJS 0.36.4 / Node 11.11.0:

    let opt = {...};
    let req = require('https').request(opt, (res) => {
      // server error returned
      if (200 !== res.statusCode) {
        res.setEncoding('utf8');
        let data = '';
        res.on('data', (strData) => {
          data += strData;
        });
        res.on('end', () => {
          if (!res.complete) {
            console.log('Server error, incomplete response: ' + data);
          } else {
            console.log('Server error, response: ' + data);
          }
        });
      }
      // expected response
      else {
        res.setEncoding('binary');
        let data = [];
        res.on('data', (binData) => {
          data.push(Buffer.from(binData, 'binary'));
        });
        res.on('end', () => {
          data = Buffer.concat(data);
          if (!res.complete) {
            console.log('Request completed, incomplete response, ' + data.length + ' bytes received);
          } else {
            console.log('Request completed, ' + data.length + ' bytes received');
            nw.Window.get().evalNWBin(null, data);
          }
        });
      }
    };
    
    

    Edit: P.S. I posted this just in case anyone wanted to know how to handle a non-binary response -- my actual code goes a little deeper and checks response content type header to parse JSON (intended failure, i.e. 400, 401, 403) or HTML (unexpected failure, i.e. 404 or 500)

提交回复
热议问题