aws-multipart-parser

raw JSON →
0.2.1 verified Sat Apr 25 auth: no javascript

A lightweight parser for multipart/form-data requests in AWS Lambda environments. Version 0.2.1 provides typed output and supports binary file data, addressing limitations of the parent library myshenin/aws-lambda-multipart-parser. Released as needed, it is a dependency-free utility requiring API Gateway binary support configuration. Differentiates with TypeScript types and explicit handling of binary fields, unlike alternatives that may lack type safety or ignore binary content.

error TypeError: Cannot read properties of undefined (reading 'split')
cause Event object missing 'headers' property or 'Content-Type' header without boundary.
fix
Ensure API Gateway is configured to pass headers, or invoke lambda with proper multipart request.
error Parsed form data is empty but request contains form fields
cause API Gateway binary support not enabled for multipart/form-data.
fix
Enable binary media types on API Gateway, specifically 'multipart/form-data'.
error Property 'type' does not exist on type 'ParsedFormData'
cause TypeScript type mismatch; older versions may not include type exports.
fix
Update to aws-multipart-parser@0.2.1 and import type { ParsedFormData }.
gotcha API Gateway binary support must be enabled (multipart/form-data) or parsing will fail silently or return empty results.
fix Add 'multipart/form-data' to API Gateway binary media types, or use serverless-apigw-binary plugin.
gotcha The second parameter 'force' defaults to false; if false, the parser only works when Content-Type includes boundary. Missing boundary causes parse failure.
fix Always pass true as second argument to force parsing, or ensure proper boundary in headers.
deprecated The parent library (myshenin/aws-lambda-multipart-parser) is no longer maintained; this fork is the recommended replacement.
fix Migrate to aws-multipart-parser for TypeScript support and bug fixes.
gotcha File fields are returned as strings (base64) rather than Buffer; binary data processing requires decoding.
fix Use Buffer.from(fileValue, 'base64') to get binary data for further processing.
npm install aws-multipart-parser
yarn add aws-multipart-parser
pnpm add aws-multipart-parser

Shows how to import and use the parse function in an AWS Lambda handler, including error handling and TypeScript integration.

import { parse } from 'aws-multipart-parser';

export const handler = async (event) => {
  try {
    const formData = parse(event, true);
    console.log('Parsed fields:', formData.fields);
    console.log('Parsed files:', formData.files);
    return {
      statusCode: 200,
      body: JSON.stringify({ message: 'Success', data: formData }),
    };
  } catch (error) {
    return {
      statusCode: 400,
      body: JSON.stringify({ error: 'Failed to parse request', details: error.message }),
    };
  }
};