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.
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 Next App Routes
, instead of Pages 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With