Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: req.formData is not a function, in next js api

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.

like image 792
Ahmer Saud Avatar asked Oct 19 '25 19:10

Ahmer Saud


1 Answers

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

like image 135
Dan Carreon Avatar answered Oct 22 '25 11:10

Dan Carreon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!