question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FormData doesn't work properly on node.js

See original GitHub issue

Describe the bug

I actually spent a lot of time debugging if this was a problem with axios, form-data, node or my framework. I narrowed it down to form-data. It actually works properly using the default FormData provided by the browser. But the thing is I need it running with node. I tried parsing it with formidable, nestjs parser and adonisjs v5 parser. All of them work properly using native formdata.

How to reproduce

Just send any FormData with axios, fetch to an endpoint that will print what you receive. It will print out something like this:

{
  "----------------------------007181389194487799053447\r\nContent-Disposition: form-data; name": "\"f\"\r\n\r\nt\r\n----------------------------007181389194487799053447--\r\n"
}

Since fetch is not native to node.js, I’m using axios for my test script:

import FormData from "form-data";
import axios from "axios";

const f = new FormData();
f.append("f", "t");
axios.post("http://0.0.0.0:3333/", f).then((data) => console.log(data.data)).catch((e) => console.warn(e));

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:15
  • Comments:12

github_iconTop GitHub Comments

10reactions
Yuval-Peledcommented, May 14, 2021

@Thymas1 I ended up working with native node.js libraries and it’s just as easy:

const data = new URLSearchParams();
data.append(key, value);
const response = await axios.post(url, data, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})

I’m not sure if this covers all of the use cases that form-data solves, but for me this was enough (and removed a dependency, to boot).

5reactions
privateOmegacommented, Jan 7, 2022

Try adding "Content-Length": formdata.getLengthSync() in headers, that solved it for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Form Data doesn't show correctly when I send it via ...
Form Data doesn't show correctly when I send it via Fetch with Node & Muller & Mongoose & GirdFS ; And what error...
Read more >
FormData doesn't work properly on node.js - Bountysource
I narrowed it down to form-data. It actually works properly using the default FormData provided by the browser. But the thing is I...
Read more >
FormData is not defined Error in JavaScript | bobbyhadz
The "FormData is not defined Error" error occurs when we try to use the FormData() constructor on the server side, most commonly in...
Read more >
FormData - Web APIs - MDN Web Docs - Mozilla
Chrome Edge FormData Full support. Chrome5. Toggle history Full support. Edge12. Toggl... @@iterator Full support. Chrome50. Toggle history Full support. Edge18. Toggl... FormData() constructor Full support....
Read more >
Can't upload a file from node.js to my route, getting empty ...
as obviously it is not reading the correct file that you are trying to upload. So use console.log and make sure that that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found