Search code examples
node.jsreactjsaws-lambdaaws-api-gatewaymultipartform-data

How to read form data in the AWS Lambda written in NodeJs?


I'm building an app using ReactJS in the front-end and back-end as AWS API Gateway/AWS Lambda written in NodeJS. I'm passing the form data of my React app in the attribute called userAttributes like below:

enter image description here

I want to read this data in the backend logic. As per the Cloud Watch logs of Lambda, I'm getting the form data in the Lambda event as:-

  body: '------WebKitFormBoundarytTytoOIkcO2\r\n' +
    'Content-Disposition: form-data; name="userAttributes"\r\n' +
    '\r\n' +
    '[object Object]\r\n' +
    '------WebKitFormBoundarytTytrEkb0oOIkcO2--\r\n',
  isBase64Encoded: false
}

Here [object Object] is my Form data. How can parse and read this form data value? I can read my other query string parameters (event.queryStringParameters.<attributes>) but not sure how to read the form data.


Solution

  • Use https://www.npmjs.com/package/parse-multipart on your lambda and you can parse the data...

    But first

    • Set the media type in you api gateway as multipart/form-data

    • When you added your lambda to the api gateway method please be sure if you check "proxy" because you need that to manage the data from the frontend as multipar/form-data