Here is the code for my nextjs route
import { NextRequest, NextResponse } from 'next/server';
export const config = {
runtime: 'edge',
};
export default async function POST(req: NextRequest): Promise<Response> {
const data = await req.formData();
console.log(data);
const file: File | null = data.get('file') as unknown as File;
if (!file) {
return NextResponse.json({ success: false });
}
return NextResponse.json({ success: true });
}
I am trying to send a file in the request with Content-type: multipart/form-data
but it throws error in next js api. What am i missing? Is there something i should consider about the egde environment, i also tried to change it to node js environment but the error remains the same.
2
Answers
Check that the req.formData() function is parsing the form data correctly.check in console you might get some insight
Not sure if this can be qualified as an answer, but I’m looking for the same solution and just found this discussion where it explains that
req.formData()
can be handled only if you are using NextApp Routes
, instead ofPages Routes
.. honestly, I’m not going to change the entire structure of my project, so I’m still looking for a solution, but if it helps you then there’s the option