title: Node.js request with zlib library
body: <p>I'm trying to figure out a way to make <a href="https://github.com/mikeal/request" rel="nofollow noreferrer">this</a> library work with gzip and deflate.</p>
  
  <p>Looked at <a href="https://stackoverflow.com/questions/8880741/node-js-easy-http-requests-with-gzip-deflate-compression">this</a> question, tried the suggestions, but for some reason it still does not work. I'm relatively new to node.js, could somebody take a look at my code and tell me what I'm doing wrong? </p>
  
  <pre><code>var request = require("request"),
  zlib = require( 'zlib' );
  var req = request( {
      uri: "http://google.com",
      headers: {
          'Accept-Encoding': 'gzip, deflate',
          'user-agent': 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1',
          },
      }, 
  function( error, res, body ) {
      var output;
      switch ( res.headers[ 'content-encoding' ] ) {
          case 'gzip':
              var gzip = zlib.createGunzip();
              res.pipe(gzip);
              output = gzip;
          break;
          case 'deflate':
              var gzip = zlib.createInflate();
              res.pipe(gzip);
              output = gzip;
          break;
          default:
              res.setEncoding( 'utf8' );
              output = res;
          break;
      }
      //console.log( body ) // outputs encoded html
      output.on('data', function ( data ) {
          console.log( data ); // never happens
      });
      output.on('end', function() {
          console.log( data ); // never happens
      });
  });
  </code></pre>