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

[BUG] multipart/form-data validation breaks the request object and gives no access to content #22

Open
paulish opened this issue Jun 8, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@paulish
Copy link

paulish commented Jun 8, 2021

/**
 * @typedef {object} UserPhotoUploadRequest
 * @property {string} field1 - some field 1
 * @property {integer} field2 - some field 2
 * @property {string} photo.required - Photo - binary
 */

/**
 * POST /photo
 * @summary Uploads use photo
 * @param {UserPhotoUploadRequest} request.body.required - request body - multipart/form-data
 * @returns {boolean} 200 - success
 * @returns {string} 400 - error text
 * @returns {string} 500 - error text
 */
router.post('/photo', validateRequest(), async (req, res) => {
    try {
        const form = new multiparty.Form();
        form.parse(req, async (err, fields, files) => {
               // here we get an error since req object is already read by the validateRequest()
               // also we have no way to access parsed fields and files by the validateRequest() call
        });
  } catch (err) {
      res.status(500).send(err.message);
  }
}

This happens since multiparty reads the req object stream. A possible solution is to modify the request object inside the validateRequest() call by adding fields and files properties there.

@kevinccbsg kevinccbsg self-assigned this Jun 8, 2021
@kevinccbsg kevinccbsg added the bug Something isn't working label Jun 8, 2021
@kevinccbsg
Copy link
Member

Thanks for the example 👍 we will investigate the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants