import FormData from 'form-data';
export async function POST(request) {
let data = await request.formData() // This line throws an error
// Do something with data
}
Building the form data itself works fine, but parsing the form data on NextJs 13’s RouteHandler is the problematic one. It works fine when i use other deployment and hosting services but fails to work on netlify.
Hi @Mozzarella99, thanks for sharing the extra information. The reason why response.formData is throwing an error is because Next.js 13 App Router is still in beta.
There are 2 ways to resolve the issue.
1 Use request.body to parse the form data
export async function GET(request) {
return new Response('Hello, Next.js!')
}
export async function POST(request) {
try {
let data = await request.body;
} catch (e) {
console.log(e)
return new Response("fail")
}
return new Response("success")
2 Use the experimental-edge configuration and send your form data as multipart/form-data OR application/x-www-form-urlencoded
// Make sure you send your form data as multipart/form-data
// OR application/x-www-form-urlencoded from the front-end.
export const config = { runtime: 'experimental-edge' }
export async function GET(request) {
return new Response('Hello, Next.js!')
}
export async function POST(request) {
try {
let data = await request.formData();
} catch (e) {
console.log(e)
return new Response("fail")
}
return new Response("success")
}
Make the changes and then redeploy again to see if it works.