form-data: FormData doesn't work properly on node.js
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));
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 15
- Comments: 15
@Thymas1 I ended up working with native node.js libraries and it’s just as easy:
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).
Try adding
"Content-Length": formdata.getLengthSync()
in headers, that solved it for me.This also doesn’t work for me. I tried downgrading from 4.0.0 to 3.0.1 and to 3.0.0 (which is 2 years old) and it did not help. I’m suspecting this is something that changed in node.js itself. My version is 14.16.1, the most recent one