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.

fails to handle 'multipart/form-data' in openapi v3 (Unsupported Content-Type multipart/form-data; boundary=...)

See original GitHub issue

i’m getting an error trying to handle file uploads with v3.6.0. The spec is:

openapi: "3.0.0"
servers:
    - url: http://localhost:3000
      description: local server

paths:
    /upload/:
        post:
            summary: Create NEW files
            operationId: upload
            requestBody:
                required: true
                content:
                    multipart/form-data:
                        schema:
                            type: object
                            properties:
                                docs:
                                    type: array
                                    items:
                                        type: string
                                        format: binary
# tried this as well but also doesnt work:
                        # schema:
                        #   type: object
                        #   properties:
                        #     upload:
                        #       type: string
                        #       format: binary

express-openapi is initialised as suggested:

 '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);
                return next(); // this executes correctly
            });
        }

Hitting the app with curl or swagger-ui: curl "http://localhost:3000/upload/" -H "accept: */*" -H "Content-Type: multipart/form-data" -F docs=@package.json fails with the following error (the endpoint never gets called):

Unsupported Content-Type multipart/form-data; boundary=----WebKitFormBoundaryWyK9kAU7d35AKf26

The same happens with superagent/supertest and postman. Any hints?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:37 (37 by maintainers)

github_iconTop GitHub Comments

2reactions
mendelkcommented, Jan 30, 2019

Thanks @mbsimonovic .

In your example, changing the spec like so:

multipart/form-data:
    schema:
        required:
            - files
        type: object
        properties:
            files:
                type: array
                items:
                    type: string
                    format: binary

(Note the required property.)

This causes the following error:

errors: [
    {
      "path": "files",
      "errorCode": "required.openapi.validation",
      "message": "should have required property 'files'",
      "location": "body"
    }
  ]

This, I believe, is the crux of our issue!

Edit, to explain further: The reason your spec is validating is because the files property is in fact not being validated, because it’s an optional property. From the validator’s point of view, that property doesn’t exist to validate. Making it a required field changes all that. Now you’ll need to put files on the req.body before x-express-openapi-additional-middleware, i.e. via args.consumesMiddleware. Once that’s done, you’ll get the same error I’ve been getting all along:

  errors: [
    {
      "path": "files[0]",
      "errorCode": "type.openapi.validation",
      "message": "should be string",
      "location": "body"
    }
  ]
2reactions
mbsimonoviccommented, Jan 30, 2019

here you go. so npm install first, then npm run test.

openapi-multipart-demo.zip

Read more comments on GitHub >

github_iconTop Results From Across the Web

The request was rejected because no multipart boundary was ...
In postman content-type="multipart/form-data" and I am getting the below exception. HTTP Status 500 - Request processing failed; nested exception is org.
Read more >
Multipart Requests - Swagger
OAS 3 This guide is for OpenAPI 3.0. Multipart Requests. Multipart requests combine one or more sets of data into a single body,...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
It is common to use multipart/form-data as a Content-Type when transferring request bodies to operations. In contrast to 2.0, a schema is ...
Read more >
Multipart (Form Data) Format - MuleSoft Documentation
shows a raw multipart/form-data payload with a 34b21 boundary consisting of 3 parts: a text/plain one named text. an application/json file ( a....
Read more >
415 Unsupported Media Type - HTTP - MDN Web Docs
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload ...
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