title: How do I ungzip (decompress) a NodeJS request's module gzip response body?
body: <p>How do I unzip a gzipped body in a request's module response?</p>
  
  <p>I have tried several examples around the web but none of them appear to work.</p>
  
  <pre><code>request(url, function(err, response, body) {
      if(err) {
          handleError(err)
      } else {
          if(response.headers['content-encoding'] == 'gzip') {    
              // How can I unzip the gzipped string body variable?
              // For instance, this url:
              // http://highsnobiety.com/2012/08/25/norse-projects-fall-2012-lookbook/
              // Throws error:
              // { [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }
              // Yet, browser displays page fine and debugger shows its gzipped
              // And unzipped by browser fine...
              if(response.headers['content-encoding'] &amp;&amp; response.headers['content-encoding'].toLowerCase().indexOf('gzip') &gt; -1) {   
                  var body = response.body;                    
                  zlib.gunzip(response.body, function(error, data) {
                      if(!error) {
                          response.body = data.toString();
                      } else {
                          console.log('Error unzipping:');
                          console.log(error);
                          response.body = body;
                      }
                  });
              }
          }
      }
  }
  </code></pre>