Open ghost opened 8 years ago
hey! Whats the solution on this?
You need to use a library like npm multer and add it to your middleware stack. This will catch the file on POST and attach it to the req object as req.files
with multer is working good but only for multiple files multer.fields([{name: 'file'}])
since multer add the req.files property but I can't make it working with single file:
multer.single('file')
will create req.file instead of req.files and swagger will throw this err:
Error: req.files must be provided for 'formData' parameters of type 'file'
at Parameter.getValue (C:\path\to\api\node_modules\sway\lib\types\parameter.js:141:15)
any idea on how to make this work?
I got a work around on it. Every http middleware stack runs http request to complete request parser which also declare req.file or req.files as array of files for some http frameworks. Just jump to (C:\path\to\api\node_modules\sway\lib\types\parameter.js:141:15) line number & edit it to req.file where key is file & parameter value will be set to it. It works for sure, thanks!
@b4dnewz : This custom middleware worked for me to handle single file upload. If req.file
is there it puts it into req.files
app.use(function (req, res, next) {
if(req.file){
req.files = req.file
}
next();
});
@rahulthakur319 nice trick
but I think this issue should be addressed from swagger-node project rather than by users, since multer is the de-facto standard for handling multipart-form-data
it's a pretty simple change:
if (_.isUndefined(req.files)) { // _.isUndefined(req.files) && _.isUndefined(req.file)
}
or even better, by adding in the swagger definition a property to specify if should be threat as multiple or single file upload
Also using req.files[0] it's a legit trick
For testing purposes i specified incorrect post methods (x-www-form-urlencoded, raw, binary) to handle multipart/form-data. A failed response is expected but an error is thrown rather than being caught by my error handler.
Spec
Stack