title: How can I gunzip POST request data in express?
body: <p>I am trying to build a server that can accept gzipped POST data with express. I think I could just write my own middleware to pipe the request stream to a zlib.createGunzip() stream. The question is, how can I achieve that, afterwards, the express.bodyParser() middleware is still able to parse my gunzipped POST data?</p>
  
  <p>I tried to replace the original request stream methods by the ones of the zlib stream, but that just made the bodyParser return a "Bad Request" Error:</p>
  
  <pre><code>var express = require('express');
  var app = express();
  
  function gUnzip(req, res, next) {
    var newReq;
    if (req.headers['content-encoding'] === 'gzip') {
      console.log("received gzipped body");
      newReq = req.pipe(zlib.createGunzip());
      Object.getOwnPropertyNames(newReq).forEach(function (p) {
        req[p] = newReq[p];
      });
    }
    next();
  }
  
  app.use(gUnzip);
  app.use(express.bodyParser());
  
  app.listen(8080);
  </code></pre>
  
  <p>Is there a way to make this work without rewriting the bodyParser() middleware within my own middleware?</p>
  
  <p>EDIT:
  This is the same question: <a href="https://stackoverflow.com/questions/14194864/unzip-post-body-with-node-express">Unzip POST body with node + express</a>. But in the answer he just does in his own middleware what the express.bodyParser() should do, which is what I want to avoid. I am looking for a way to simply unzip the request data from the stream and then pass it to the bodyParser(), which expects a stream itself, as can be seen at <a href="http://www.senchalabs.org/connect/json.html" rel="nofollow noreferrer">http://www.senchalabs.org/connect/json.html</a>.</p>