Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing data in body (ignored other values to files) #6

Open
VeeeneX opened this issue Jul 16, 2020 · 3 comments
Open

Missing data in body (ignored other values to files) #6

VeeeneX opened this issue Jul 16, 2020 · 3 comments

Comments

@VeeeneX
Copy link

VeeeneX commented Jul 16, 2020

We're sending data as in example bellow, due to this line:

     if (request.raw.files) {
        for (const key in request.raw.files) {
          request.body[key] = request.raw.files[key]
        }
      }

They got ignored and therefore not added into body, so I hope this helps somebody, @huangang maybe consider adding following 🙂

import fp from 'fastify-plugin'
import fileUpload from 'express-fileupload'

const setMultipart = (request, done): void => {
  request[Symbol('multipart')] = true
  done()
}

const fastifyUpload = (fastify, options, done) => {
  fastify.addContentTypeParser('multipart', setMultipart)

  options = options || {}
  fastify.use(fileUpload(options))

  fastify.addHook('preValidation', (request, reply, done) => {
    if (request.raw) {
      !request.body && (request.body = request.raw.body || {})
      if (request.raw.files) {
        request.body = { ...request.raw.body, ...request.raw.files }
      }
    }
    done()
  })
  done()
}

export default fp(fastifyUpload, {
  fastify: '>= 2.0.0',
  name: 'fastify-file-upload'
})

Example request

PUT http://localhost:3000/registration/938cecf7-fa10-4580-a4bd-978ca34f3b67
Content-Type: multipart/form-data; boundary=WebAppBoundary
x-token: vDkhBTJCwsdov0t55bzu4UHIN9AXZgYJGz3Fq326Rske8ZCm

--WebAppBoundary
Content-Disposition: form-data; name="files"; filename="logo.png"
Content-Type: image/png

< ./input.pdf

--WebAppBoundary
Content-Disposition: form-data; name="files"; filename="logo.png"
Content-Type: image/png

< ./input.png

--WebAppBoundary
Content-Disposition: form-data; name="data"

{}
--WebAppBoundary--
@huangang
Copy link
Owner

Can you provide an example repositories? I want to try

@niumowangorz
Copy link

Suppose the backend needs to receive the imageKey field, array type, and the frontend uses formdata to pass it.

imageKey has strings and streams of files,

Now that raw.files overwrites the body, the string part of imageKey is missing.

@niumowangorz
Copy link

4.0.0 also has bugs, waiting to be updated by the author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants