Skip to content

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

@paulish

Description

@paulish
/**
 * @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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions