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.

fastify-multipart v 3.0 not able to access key/value pairs with upload binary file

See original GitHub issue

🐛 Bug Report

Thanks for improving this plugin to make it easier for files upload. I’ve switched over to v3.0 and tried out the new API but it’s not letting me access the fields data like the legacy version

To Reproduce

Steps to reproduce the behavior:

Client form

    const formData = new FormData();
    formData.append("avatar", file, file.name); // <input type="file" />
    formData.append("testData", JSON.stringify({ name: "Foo" })); 

    fetch("/", {
      method: "POST",
      body: formData
    })

Server

    fastify.post('/', async function (req, reply) {
        const data = await req.file()

        data.file // stream
        data.fields // other parsed parts
        data.fieldname
        data.filename
        data.encoding
        data.mimetype

        console.log(JSON.parse(data.fields))

        await pump(data.file, fs.createWriteStream(data.filename))

        reply.send()
    })

Expected behavior

print out { name: ‘Foo’ }

in the legacy version, i could access it under this:

  mp.on('field', function (key, value) {
    console.log(JSON.parse(value))
  })

Your Environment

  • node version: 12
  • fastify version: >=3.0.0
  • os: Mac

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
StarpTechcommented, Aug 27, 2020

We can’t parse out of sync because files must be handled first. In order to achieve your result, you have to reorder your form

	formData.append("testData", JSON.stringify({ name: "Foo" }));
    formData.append("avatar", file, file.name); // <input type="file" /> 
1reaction
StarpTechcommented, Aug 27, 2020

or you use multipartIterator() to iterate over field and files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fastify Multipart File Upload - Backend Cafe
Fastify manages multipart form data to save uploaded files. Upload your files using axios or fetch.
Read more >
How to handle Node.js file uploads with Fastify - Snyk
To do this, you need to import multipart from '@fastify/multipart' and then register it. Here you registered multipart on the Fastify app you ......
Read more >
formzilla - npm
Fastify plugin for parsing multipart/form data. Latest version: 3.0.0, last published: 12 hours ago. Start using formzilla in your project ...
Read more >
Nestjs - file upload with fastify multipart - Stack Overflow
2 Answers 2 · i need to use fastify with nestjs to post fields(not files) multipart data. In both my application and your...
Read more >
Uploading a File to a Server from Flutter Using a Multi-Part ...
Usually, the body of a POST request is made of textual key-value pairs. With a multipart POST request, you can also include files...
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