AWS Lambda Multipart Form Data Parser
This library provides a specialized parser for `multipart/form-data` requests specifically designed for AWS Lambda environments. Unlike general-purpose server-based parsers, `aws-lambda-multipart-parser` is tailored to work within the constraints of serverless functions and the particular event structure provided by AWS API Gateway. It takes the raw `event` object from a Lambda invocation and processes its `body` to extract form fields and uploaded files, returning a structured JavaScript object. The current stable version, 0.1.3, indicates a very early stage of development, and the project appears to be abandoned, with no significant updates or commits in several years. Its primary differentiator was its early focus on simplifying `multipart/form-data` handling for AWS Lambda at a time when native support was less mature, abstracting away the complexities of binary media types, base64 encoding, and API Gateway proxy integration nuances.
Common errors
-
502 Error: Bad Gateway
cause API Gateway is not configured for Lambda Proxy Integration, or `integration: LAMBDA` is missing in Serverless Framework configuration.fixIn API Gateway console, for your POST method's 'Integration Request', check 'Use Lambda Proxy Integration'. If using Serverless Framework, add `integration: LAMBDA` to your function's HTTP event configuration. -
Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause Your Lambda function's response is missing the `Access-Control-Allow-Origin` header required for cross-origin requests.fixInclude `headers: { "Access-Control-Allow-Origin": "*" }` (or your specific frontend origin) in your Lambda function's response object.
Warnings
- breaking This library is effectively abandoned, with no updates or commits in over six years. It may not be compatible with newer Node.js runtimes (e.g., Node.js 18+), latest AWS Lambda features, or API Gateway configurations. Modern alternatives should be preferred.
- gotcha When integrating with API Gateway, it is crucial to set `integration: LAMBDA` in your `serverless.yml` or enable 'Use Lambda Proxy Integration' in the API Gateway console. Failing to do so will result in '502 Bad Gateway' errors.
- gotcha Default API Gateway CORS configurations do not always work correctly with `LAMBDA` integration for `multipart/form-data`. You must manually include `Access-Control-Allow-Origin` headers in your Lambda function's response.
- gotcha The library returns file contents as Node.js `Buffer` objects by default for non-text files. If `spotText` is `true`, text files are returned as strings. Handling binary buffers correctly (e.g., converting to base64 for JSON responses) is crucial.
Install
-
npm install aws-lambda-multipart-parser -
yarn add aws-lambda-multipart-parser -
pnpm add aws-lambda-multipart-parser
Imports
- parse
import parse from 'aws-lambda-multipart-parser';
const parse = require('aws-lambda-multipart-parser'); - parse
import { parse } from 'aws-lambda-multipart-parser';const { parse } = require('aws-lambda-multipart-parser');
Quickstart
const multipart = require('aws-lambda-multipart-parser');
exports.handler = (event, context, callback) => {
// Ensure API Gateway is configured for binary media types and Lambda Proxy Integration.
// The 'spotText' parameter can be set to true to have text files returned as strings.
const parsedBody = multipart.parse(event, true);
// Example of accessing a field and a file
console.log('Form field value:', parsedBody.field);
if (parsedBody.file) {
console.log('File:', parsedBody.file.filename, parsedBody.file.contentType);
// If spotText is true, content of text files is a string
console.log('File content (text):', parsedBody.file.content);
}
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*", // Essential for CORS if frontend is different origin
"Content-Type": "application/json"
},
body: JSON.stringify({
message: "Successfully parsed multipart data",
data: parsedBody // Be careful exposing raw parsed data, especially buffers
})
};
callback(null, response);
};