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.

Unable to populate form-data

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
GrosSacASaccommented, Jan 21, 2021

files from form.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

1reaction
Romick2005commented, Jul 3, 2020

Have tried add files using readStream? Smth like: formData.append(‘file’, fs.createReadStream(file.path));

Read more comments on GitHub >

github_iconTop 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 >

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