DEV Community

İbrahim Turan
İbrahim Turan

Posted on

How to parse FormData values with Netlify Functions V2

I spend 3 hours trying to understand that. All the blog posts related to this topic do not work. Netlify Functions V2 uses Request API. I used Node.js v20 to parse FormData values in multipart/form-data requests. You can see how easy it is with the example below. This function returns form data values as JSON in response.

export default async function (event) {
  const data = await event.formData();
  const fields = {};

  for (const pair of data.entries()) {
    fields[pair[0]] = pair[1];
  }

  return new Response(JSON.stringify(fields), { status: 200 });
}
Enter fullscreen mode Exit fullscreen mode

I hope this helps you. Have nice coding!

Top comments (0)