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.

File upload not working

See original GitHub issue

Hello,

I have an endpoint documented like this:

post.apiDoc = {
    description: 'Upload an image for the category',
    operationId: 'uploadCategoryImage',
    tags: ['categories'],
    consumes: ['multipart/form-data'],
    parameters: [
        {
            name: 'image',
            in: 'formData',
            type: 'file',
            description: 'An image for this category',
            required: true
        }
    ],
    responses: {
        201: {
            description: 'Image successfully uploaded'
        },
        default: {
            description: 'Unexpected error',
            schema: {
                $ref: '#/definitions/Error'
            }
        }
    }
};

In the Swagger UI, a file upload thing is visible:

swagger

However, I keep getting this error when I send a file:

{
    path: 'image',
    errorCode: 'required.openapi.validation',
    message: 'instance requires property "image"',
    location: 'body'
}

I do have a Content-Type: multipart/form-data header in the HTTP request. Am I doing something wrong or is it a bug?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:16 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
alrikcommented, Jan 2, 2018

I got it to work with multer and a little tweak. Hopefully the following examples will be helpful for some people.

Multer is a multipart form-data handling middleware. by default it stores the upload to req.file or req.files. The express-openapi-validation logic is looking for defined files in req.body. So we need to copy the file upload to the expected position.

expressOpenapi.initialize({
  consumesMiddleware: {
    // example 1: Allow for single file upload (filename: 'file')
    'multipart/form-data'(req, res, next) {
      multer().single('file')(req, res, (err) => {
        if (err) return next(err);
        req.body.file = req.file;
        next();
      });
    }

    // example 2: Allow for any file upload
    'multipart/form-data'(req, res, next) {
      multer().any()(req, res, (err) => {
        if (err) return next(err);
        req.files.forEach(f => req.body[f.fieldname] = f);
        next();
      });
    }
  },
});
1reaction
jsdevelcommented, May 4, 2016

@hkors there’s currently no support for file uploads. It would be great if you could propose a solution. Most swagger frameworks that I’ve seen don’t handle this very well. I’d love express-openapi to be one of the first!

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Upload not working, how do i fix this? - Microsoft Community
1) Reset the browsers in Windows Settings > Apps & Features > Browser > Advanced Options > Reset and Repair to see if...
Read more >
forms - File upload not working | Php - Stack Overflow
As soon as I use enctype="multipart/form-data", nothing works at all the page simply refreshes. But, when I'm not using enctype="multipart/form ...
Read more >
I Can't Upload My Files - Strikingly Help Center
I Can't Upload My Files · Step 1: Check your file · Step 2: Exit the editor and try again · Step 3:...
Read more >
How to Solve File Upload Errors in Google Drive - MakeUseOf
How to Solve File Upload Errors in Google Drive: 8 Quick Solutions · 1. Check Google Drive's Status · 2. Check Your Network...
Read more >
Troubleshooting file upload problems - UK Copyright Service
Make sure the file is not corrupted. · Copy the file to your home/desktop folder and upload from there. · Don't upload folders...
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