How to use request or http module to read gzip page into a string

前端 未结 5 1316
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 05:09

I found the request module in js cannot handle gzip or inflate format http response correctly.

for example:

request({url:\'some url\'}, function (err         


        
相关标签:
5条回答
  • 2020-12-09 05:46

    I ran into a similar issue and wanted to continue using the request library instead of the built-in http module. I've discussed two working approaches here: http://nickfishman.com/post/49533681471/nodejs-http-requests-with-gzip-deflate-compression. One of them is similar to @Teemu's answer, while the other uses streams.

    0 讨论(0)
  • 2020-12-09 05:50

    request module handles the gzip responses. All we have to do is to set 'gzip' attribute in the opts. For detailed explaination please visit the below linke. There I have clearly explained with example.

    https://stackoverflow.com/a/38582506/5878471

    0 讨论(0)
  • 2020-12-09 05:53

    The answers of @Dawid and @Teemu sometimes brake the chars in the answer in case of utf-8 encoding. This code works much better:

    function getGzipped(url, cb) {
        // downloads gzipped file
        http.get(url, function(res) {
    
            let chunks = [];
    
            res.on('data', function(chunk) {
                chunks.push(chunk);
            });
            res.on('end', function() {
                let buffer = Buffer.concat(chunks);
                zlib.gunzip(buffer, function(err, decoded) {
                    if (err) throw err;
                    cb(decoded && decoded.toString());
                });
            });
        });
    }
    
    0 讨论(0)
  • 2020-12-09 06:01

    simplified example:

    var https = require('https');
    var gunzip = require('zlib').createGunzip();
    
    var options = {
        host: 'api.stackexchange.com',
        path: '/2.1/info?site=stackoverflow'
    };
    
    https.get(options, function(res) {
      var body = '';
    
      res.pipe(gunzip);
    
      gunzip.on('data', function (data) {
          body += data;
      });
    
      gunzip.on('end', function() {
          console.log(JSON.parse(body));
      });
    });
    
    0 讨论(0)
  • 2020-12-09 06:07

    Pipe the response to the gzip stream and use it as you would use the original response object.

    var req = http.request(options, function(res) {
        var body = "";
    
        res.on('error', function(err) {
           next(err);
        });
    
        var output;
        if( res.headers['content-encoding'] == 'gzip' ) {
          var gzip = zlib.createGunzip();
          res.pipe(gzip);
          output = gzip;
        } else {
          output = res;
        }
    
        output.on('data', function (data) {
           data = data.toString('utf-8');
           body += data;
        });
    
        output.on('end', function() {
            return next(false, body);
        });
     });
    
    req.on('error', function(err) {
       next(err);
    })
    
    0 讨论(0)
提交回复
热议问题