File Upload
Swagger 2.0 supports file uploads sent with Content-Type: multipart/form-data
. That is, your API server must consume multipart/form-data
for this operation:
The operation payload is defined using formData
parameters (not body parameters). The file parameter must have type: file
:
This definition corresponds to the following HTTP request:
Swagger UI displays file parameters using a file input control, allowing the users to browse for a local file to upload.
Upload a File + Other Data
File parameters can be sent along with other form data:
The corresponding HTTP request payload will include multiple parts:
Multiple Upload
You can have several named file parameters, each defined individually:
However, uploading an arbitrary number of files (an array of files) is not supported. There is an open feature request at https://github.com/OAI/OpenAPI-Specification/issues/254. For now, you can use a binary string array as a workaround for uploading an arbitrary number of files:
Note that this will not produce the file upload interface in Swagger UI.
FAQ
Can I upload files via PUT?
Swagger 2.0 supports file upload requests with Content-Type: multipart/form-data
, but does not care about the HTTP method. You can use POST, PUT or any other method, provided that the operation consumes multipart/form-data
. Uploads where the payload is just the raw file contents are not supported in Swagger 2.0, because they are not multipart/form-data
. That is, Swagger 2.0 does NOT support something like:
Note also that file uploading in Swagger UI only works for POST requests, because HTML forms in browsers support GET and POST methods only.
Can I define the Content-Type for uploaded files?
This is supported in OpenAPI 3.0, but not in OpenAPI/Swagger 2.0. In 2.0, you can say that an operation accepts a file, but you cannot say that this file is of a specific type or structure.
As a workaround, vendor extensions may be used to extend this functionality, for example:
Did not find what you were looking for? Ask the community
Found a mistake? Let us know