How do I ungzip (decompress) a NodeJS request's module gzip response body?

前端 未结 9 1165
日久生厌
日久生厌 2020-11-27 04:21

How do I unzip a gzipped body in a request\'s module response?

I have tried several examples around the web but none of them appear to work.

request(         


        
9条回答
  •  清酒与你
    2020-11-27 04:50

    Here's a working example (using the request module for node) that gunzips the response

    function gunzipJSON(response){
    
        var gunzip = zlib.createGunzip();
        var json = "";
    
        gunzip.on('data', function(data){
            json += data.toString();
        });
    
        gunzip.on('end', function(){
            parseJSON(json);
        });
    
        response.pipe(gunzip);
    }
    

    Full code: https://gist.github.com/0xPr0xy/5002984

提交回复
热议问题