Skip to content

Instantly share code, notes, and snippets.

@josefaidt
Created April 3, 2024 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefaidt/965241c25e7feb3cd97a757809b0f446 to your computer and use it in GitHub Desktop.
Save josefaidt/965241c25e7feb3cd97a757809b0f446 to your computer and use it in GitHub Desktop.
Parse a URL encoded form event in AWS Lambda
import type { APIGatewayProxyEventV2 } from "aws-lambda"
import busboy from "busboy"
// https://github.com/francismeynard/lambda-multipart-parser/blob/master/index.js
async function parseUrlEncodedFormEvent<T>(
event: APIGatewayProxyEventV2,
): Promise<T> {
return new Promise((resolve, reject) => {
const fields: Record<string, unknown> = {}
const bb = busboy({ headers: event.headers })
bb.on("field", (name, value, info) => {
fields[name] = value
})
bb.on("finish", () => {
resolve(fields as T)
})
bb.on("error", reject)
const encoding = event.isBase64Encoded ? "base64" : "binary"
bb.write(event.body, encoding)
bb.end()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment