error Error [TypeError]: request.formData is not a function #53827
-
SummaryProblemI am uploading a file using the I cannot call Package JSON Dependencies{
// ...
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/material": "^5.13.4",
"@types/file-saver": "^2.0.5",
"@types/node": "20.2.5",
"@types/react": "18.2.9",
"@types/react-dom": "18.2.4",
"@types/validator": "^13.7.17",
"eslint": "8.42.0",
"eslint-config-next": "13.4.4",
"file-saver": "^2.0.5",
"next": "^13.4.13",
"react": "18.2.0",
"react-dom": "18.2.0",
"replace-in-file": "^7.0.1",
"typescript": "5.1.3",
"validator": "^13.9.0"
}
// ...
} Client Codeconst form_data = new FormData();
// Required Data
form_data.append("application_id", current_application_id_string);
form_data.append("application_name", current_application_name_string);
form_data.append("application_url", current_application_url_string);
// Optional Data
if (form_state.application_icon !== null) {
form_data.append(
"application_icon",
form_state.application_icon,
form_state.application_icon.name
);
}
const api_url = `${api_hostname}/${api_path}`;
const fetch_promise = fetch(api_url, {
method: "POST",
headers: {
"Content-Type": "multipart/form-data",
},
body: form_data,
});
fetch_promise
.then((response) => {
// ...
})
.catch((error: any) => {
// ...
}); Server Codeimport { NextApiRequest, NextApiResponse } from "next";
async function submit_package_request(request: NextApiRequest, response: NextApiResponse) {
console.log(request_data);
const request_data = await request.formData();
// ...
} Additional informationI feel like I'm taking crazy pills.
I see a lot of references to "just use formidable". I'm open to that avenue, but I also see people saying "just use request.formData"
I cannot find documentation about the `request.formData()` approach.
And I can't find anything to indicate what the issue is here.
I have updated to the latest `Next` library. My code looks sane.
What am I doing wrong?
Why can I not get this file on the API side? ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 7 replies
-
Ok... So this is a huge headache. For anyone who finds this. The issue was several things.
In order to fix this I had to move my api endpoints from the THEN I had to rename my function to AND THEN I had to set the configuration to not use the body parser import { NextApiRequest, NextApiResponse } from "next";
export const config = {
api: {
bodyParser: false,
},
};
export async function POST(request: NextApiRequest, response: NextApiResponse) {
console.log(request_data);
const request_data = await request.formData();
// ...
} If I can say anything about this whole experience is that the NextJS team is doing a terrible job with documentation and their migration strategy for these changes. It has been an incredibly frustrating experience to chase this down and even more so to debug and triage this was a nightmare. Good luck to anyone else who finds themselves in this situation as well. |
Beta Was this translation helpful? Give feedback.
-
I ran into this issue as well. However, I was able to get it working with the help of the formidable-serverless library.
|
Beta Was this translation helpful? Give feedback.
-
I don't mean to be ignorant of the complexity here, but why is not possible to handle FormData natively within the standard |
Beta Was this translation helpful? Give feedback.
-
literally, i stucked in this from 2 days. godddd |
Beta Was this translation helpful? Give feedback.
-
I'm using |
Beta Was this translation helpful? Give feedback.
-
I am having the same issue. I am using NextJS |
Beta Was this translation helpful? Give feedback.
-
There's another issue related to this. The default fetch API actually accepts a However, if you set |
Beta Was this translation helpful? Give feedback.
Ok... So this is a huge headache.
For anyone who finds this. The issue was several things.
request.formData
without usingroute handlers
In order to fix this I had to move my api endpoints from the
repo_root_dir/src/pages/api/v1/package/submit.tsx
directory torepo_root_dir/src/app/api/v1/package/submit/route.ts
THEN
I had to rename my function to
POST
AND THEN I had to set …