AWS Lambda Multipart Form Data Parser

0.1.3 · abandoned · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates parsing an AWS Lambda event containing multipart/form-data and returning a structured JSON response, including necessary CORS headers.

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);
};

view raw JSON →