Search code examples
javascriptnode.js.netaws-lambdaaws-api-gateway

How to use nodejs's zlib to unzip GzipStream from API gateway?


Im using API gateway from AWS. Data comein is the binary format from somebody's post request. And we know it is created by :https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=netframework-4.6

body: `\x1F�\b\x00\x00\x00\x00\x00\x04\x00��]o�0\x14���oG��\x0F\x02��t�&�\x1B"\x19�6��I��7'����\x10�}v\n` +`\x1F�\b\x00\x00\x00\x00\x00\x04\x00��]o�0\x14���oG��\x0F\x02��t�&�\x1B"\x19�6��I��7'����\x10�}v\n` 
    ....,
  isBase64Encoded: false
...
'Content-Type': 'application/gzip',

My current attempt

const B = new Uint8Array(Buffer.from(body, 'binary'));
// or const B = Buffer.from(body, 'binary'); not work

zlib.unzip(B, (err, buffer) => {
  if (!err) {
    console.log(buffer.toString());
  } else {
    console.log(err);
  }
});

And getting some err:

Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (node:zlib:189:17) {
  errno: -3,
  code: 'Z_DATA_ERROR'
}

Expected

Unzip this data and parse to json with zlib from nodejs.


Solution

  • Problem solved :

    Reference this article, in setting of API gateway, enable this enter image description here

    https://techblog.commercetools.com/gzip-on-aws-lambda-and-api-gateway-5170bb02b543