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.

Allow different Content Types in File type parameters

See original GitHub issue

For multipart uploads that contain a file and some other part, like a JSON object, it would be very useful to be able to specify a content type on the File parts other than the actual binary file. This is critical for Java applications that need the content type of the part in order to know how to deserialize the json or xml into a Java object.

For what its worth, I was able to get this working by modifying swagger-ui,js to use a Blob as the FormData. I had to add a “contentType” field to the Parameter object. I know this violates the spec, but this worked for my project.

swagger-ui.js line 1797:

if (param.paramType === 'form') {
      if (param.type.toLowerCase() !== 'file' && map[param.name] !== void 0) {
        if(typeof param.contentType !== 'undefined') {
            bodyParam.append(param.name, new Blob([map[param.name]], { type: param.contentType}));
        }
        else {
            bodyParam.append(param.name, map[param.name]);
        }
      }
    }

In the swagger doc, I can define the file and the json object parameters like so:

        {
         "name": "story",
         "description": "The story object in json format",
         "required": true,
         "paramType": "form",
         "type": "Story",
         "contentType": "application/json"
        },
        {
         "name": "file",
         "description": "The base64 encoded multipart file attachment. ",            
         "required": true,
         "paramType": "form",
         "type": "File"
        }

Refer to this discussion / issue in swagger-core for more detail - https://github.com/swagger-api/swagger-core/issues/636

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:4
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
jmdacruzcommented, Apr 13, 2016

This is a very common pattern on REST APIs when you have to upload some binary content and its related metadata in JSON. In those cases, it is desirable to be able to define:

  • The Content-Type for the overall request, which could be anything on the multipart family (e.g., multipart/related, multipart/mixed, multipart/alternative). The distinction in this family of types is important, since content is not handled in the same way depending on the subtype, and you must be able to use Content Negotiation later on to retrieve each of the portions of the request.
  • The Content-Type for each individual part of the payload, e.g., application/json for the JSON object which is represented with a JSON schema in Swagger, application/octet-stream for a binary file, application/javascript for a JavaScript file, etc.
0reactions
webroncommented, Mar 3, 2017

You can do that now!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MIME types (IANA media types) - HTTP - MDN Web Docs
Each type has its own set of possible subtypes. A MIME type always has both a type and a subtype, never just one...
Read more >
Turn on support for multiple content types in a list or library
Go to the library or list for which you want to enable multiple content types. · In the SharePoint library, select the Add...
Read more >
4 The Content-Type Header Field
The Content-Type header field is used to specify the nature of the data in the body of ... The set of meaningful parameters...
Read more >
Does the HTTP Protocol support multiple content types in ...
so it takes a single media-type, which is defined by the grammar you ... values, and it is also not allowed to send...
Read more >
Media Types - Internet Assigned Numbers Authority
Per Section 12.5.1 of [RFC9110], use of the "q" parameter name to control content negotiation would interfere with any media type parameter ......
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