myshenin / aws-lambda-multipart-parser

Parser of multipart/form-data requests for AWS Lambda
MIT License
74 stars 38 forks source link

Not able to parse the Gzip form data #19

Open kalyanjanaki opened 6 years ago

kalyanjanaki commented 6 years ago

Hello

Am trying to POST the gzip form-data. After the library parses the data, Am not able to convert the buffer to string.

Below is the code am using for testing.

`const multipart = require('aws-lambda-multipart-parser'); var zlib = require('zlib');

exports.handler = function(event,context,callback){ console.log(event);

var buf = Buffer.from(event['body'], 'base64').toString();
console.log(buf)

var parsed = multipart.parse(event, true);

console.log(parsed);

var gbuf = parsed['payload']['content'];
var stringBuf = zlib.gunzipSync(buf);
console.log(stringBuf);
const response = {
    statusCode: 200,
    headers: {
        "Access-Control-Allow-Origin": "*"
    },
    body: JSON.stringify({ name : 'kalyan' })
};

callback(null,response);

}`

below is output of parser

{ name: 'kalyan',
payload: 
{ type: 'file',
filename: 'sample_payload.xml.gz',
contentType: 'application/x-gzip',
content: <Buffer 1f 8b 08 08 ef aa 47 5b 00 03 73 61 6d 70 6c 65 5f 70 61 79 6c 6f 61 64 2e 78 6d 6c 00 ec 5d 6b 73 e2 38 d6 fe be 55 ef 7f 48 f5 d7 61 07 c9 b2 7c 49 ... > } }

and below is error when i try to unzip the buffer in above content. It is failing at the below line.

var stringBuf = zlib.gunzipSync(buf);

Error: incorrect header check
at Zlib._handle.onerror (zlib.js:370:17)
at Gunzip.Zlib._processChunk (zlib.js:540:30)
at zlibBufferSync (zlib.js:239:17)
at Object.exports.gunzipSync (zlib.js:175:10)
at exports.handler (/var/task/index.js:19:26)
bu-ra3y commented 3 years ago

hey @kalyanjanaki , I think I hit the same issue. At long last I figured this out.

When the Request Body is Base64 encoded (by API Gateway, for example), the decoding you're doing is right, but when you turn the Buffer into a String you need to use binary encoding for the String instead of the default utf8:

const buf = Buffer.from(event['body'], 'base64').toString('binary);

@myshenin I think you should add this to the documentation.