body-parser icon indicating copy to clipboard operation
body-parser copied to clipboard

Define custom content encoding handlers

Open dougwilson opened this issue 10 years ago • 4 comments

Right now, body-parser provided out-of-the-box support for gzip, deflate, and identitiy content encodings. This is nice, but we can do even better by allowing users to define custom content encoding hooks that return a stream of the decoded content when provided a stream of encoded content.

dougwilson avatar Jun 14 '15 22:06 dougwilson

Any progress on this? Certain 3rd party API send us webhooks in utf-8 encoding, and bodyparser being in front of all routes forbid us to read their payload. How do I allow "utf-8" content encoding for now?

artofspeed avatar Aug 30 '17 11:08 artofspeed

Also if you set Content-Encoding as "gzip, deflate", it will throw an "unsupported content encoding in the contentstream() method. This method performs a switch case directly matching the header value with the cases "deflate', 'gzip' and 'identity', thus failing if the header value is e.g. the one referred before.

sergio-domingues avatar Jun 27 '19 12:06 sergio-domingues

Hi @dougwilson - I have a similar situation wherein the upstream system is sending 'Content-Encoding' header with value = UTF-8 - this is getting rejected by the body-parser upfront. I had previously added the following in correlation-middleware (and included in main.ts) but after version upgrade for nestJs, even this check is not effective anymore.

main.ts - 
  app.use(correlationMiddleware);

correlation.middleware.ts - 
  // https://github.com/expressjs/body-parser/issues/100
  if (req.headers['content-encoding']) {
    delete req.headers['content-encoding'];
  }

Any help in this regard would save the day.

sachinlala avatar Jun 13 '22 21:06 sachinlala

Hi @dougwilson - I have a similar situation wherein the upstream system is sending 'Content-Encoding' header with value = UTF-8 - this is getting rejected by the body-parser upfront. I had previously added the following in correlation-middleware (and included in main.ts) but after version upgrade for nestJs, even this check is not effective anymore.

main.ts - 
  app.use(correlationMiddleware);

correlation.middleware.ts - 
  // https://github.com/expressjs/body-parser/issues/100
  if (req.headers['content-encoding']) {
    delete req.headers['content-encoding'];
  }

Any help in this regard would save the day.

I was able to resolve this by ensuring that the app.use(correlationMiddleware) directive is before the other parser settings.

As below:

Before:

  app.use(json({ limit: '1mb' }));
  app.use(urlencoded({ extended: true, limit: '1mb' }));
  app.use(correlationMiddleware);

After:

  app.use(correlationMiddleware);
  app.use(json({ limit: '1mb' }));
  app.use(urlencoded({ extended: true, limit: '1mb' }));

sachinlala avatar Jun 13 '22 21:06 sachinlala