How to parse the Content-type of the data being uploaded through aws Api gateway in aws Lambda (node.js)?

kuldeep Source

I am creating a solution where files and form-data are submitted from html-form to Api gateway and then to AWS lambda for processing and these data can be of "metapart/form-data" or "application/x-www-form-urlencoded" and also in my Api gateway binary support is already configured.I googled it but could not find which node.js library to use to parse the header Content-type.Below is the code I am using:

enter code here

const AWS = require('aws-sdk'); 
var s3 = new AWS.S3({apiVersion: '2006-03-01'});
const querystring = require('querystring'); 

AWS.config.region = 'xxx'; 

// "exports.handler" must match the entrypoint defined in the lambda Config.
exports.handler = function (event, context, callback) {
var bodyBuffer = new Buffer (String(event ['body-json']),'base64');
const params1 = querystring.parse(event.body); 

var param = {Bucket:'mybucket', Key:'abc.csv', Body: bodyBuffer};
    s3.upload(param, function(err, bodyBuffer) {
        if (err) {
        console.log(err, err.stack); // an error occurred
        else console.log(bodyBuffer);           // successful response

        console.log('actually done!');
     const my_field1 = params1['my-field1']; 

const html = `<!DOCTYPE html><p>You said: ` + my_field1 + `</p>`; 
    callback (null, "textfield1Notfilled");

 callback(null, html); 

(For now this code uploads the file to S3 bucket and displays the formdata on html page) Please help!!



answered 6 months ago agarwalankur85 #1

You can try using event.headers to get all the request headers and then use event.headers['Content-Type'].

comments powered by Disqus