Unable to populate form-data
See original GitHub issueI’m a newbie to this library, so apologies if this is too basic of a question. I am trying to populate an instance of FormData before sending it to another server using axios like so:
form.parse(req, async (err, fields, files) => {
if (err) {
errorHandler(err);
return;
}
const formData = new FormData();
Object.keys(files).forEach((key) => {
console.log(key, files[key]) // files are logged correctly.
formData.append(key, files[key]);
});
console.log(formData.datasize);
try {
const resp = await axios({
url: `http://my-url.com`,
method,
params: req.query,
data: formData,
headers: formData.getHeaders()
});
res.send(resp.data);
} catch (e) {
res.sendStatus(500).end();
}
return;
});
The problem here is that, the console.log statement above is printing ‘0’ which would mean that the form data is still empty even after calling append. Why is this happening? My diagnosis is that the above is happening because the form-data package cannot make sense of the format in which the files are returned by formidable. How do I do this?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Can't populate FormData object - Stack Overflow
Try console.log(document.forms[0].bar.value); // gets value from textbox. Create your own form data: var formData = new FormData(); // this ...
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 >form-data - npm
A library to create readable "multipart/form-data" streams. Can be used to submit forms and file uploads to other web applications.
Read more >Using FormData Objects Effectively - YouTube
This video explains how you can use a FormData object to quickly and easily grab all the data from ... Your browser can't...
Read more >Using FormData Objects - Web APIs | MDN
The number assigned to the field "accountnum" is immediately converted into a string by the FormData.append() method (the field's value can be a ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
files
fromform.parse
are not files, but representation of files.To make this work use fs.createReadStream(file.path) like @Romick2005 suggested or use examples/log-file-content-to-console.js and replace console.log statements with Buffer.concat to build the entire buffer into memory and then send those buffers
Have tried add files using readStream? Smth like: formData.append(‘file’, fs.createReadStream(file.path));