Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multipart/form-data on API Gateway/Lambda

I tried few methods, not able to get it working.

Client side(React), I am sending a zip file as follows using POST,

const data = new FormData();
        data.append('file', file);
        data.append('filename', file.name);

let params = {
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            body: data
        };

Server side:(API Gateway/Lambda/Nodejs) I added 'multipart/form-data' to Binary Media Type on Gateway side.

When parsing through lambda event.body is not well formed. It looks like this:

{"body": "e30=",
"isBase64Encoded": true }

Any ideas what might be happening? Any takes on how to parse?

like image 396
user3900196 Avatar asked Nov 18 '25 04:11

user3900196


2 Answers

Although Ariz's answer is correct, I strongly recommend you to look into AWS Pre-Signed Upload URLs. It allows your clients to upload the file first to an AWS S3 Bucket, from where your lambda function can later access the object.

Especially when you're working with large binary files, the former approach can lead to a lot of problems (-> memory issues, which is sparse in Lambda).

I have written a short blog post about this in the past.

like image 150
ampunix Avatar answered Nov 19 '25 18:11

ampunix


you are getting base64 encoded data, following is one of the ways to decode. However it's an empty object.

var base64 = 'e30='
var decodedData = Buffer.from(base64, 'base64').toString();

console.log(decodedData)
like image 42
AZ_ Avatar answered Nov 19 '25 17:11

AZ_