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:
1consumes:2 - multipart/form-data
The operation payload is defined using formData
parameters (not body parameters). The file parameter must have type: file
:
1paths:2 /upload:3 post:4 summary: Uploads a file.5 consumes:6 - multipart/form-data7 parameters:8 - in: formData9 name: upfile10 type: file11 description: The file to upload.
This definition corresponds to the following HTTP request:
1POST /upload2Host: example.com3Content-Type: multipart/form-data; boundary=abcde123454Content-Length: 2045
6--abcde123457Content-Disposition: form-data; name="upfile"; filename="example.txt"8Content-Type: text/plain9
10File contents go here.11--abcde12345--
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:
1parameters:2 - in: formData3 name: upfile4 type: file5 required: true6 description: The file to upload.7 - in: formData8 name: note9 type: string10 required: false11 description: Description of file contents.
The corresponding HTTP request payload will include multiple parts:
1POST /upload2Host: example.com3Content-Type: multipart/form-data; boundary=abcde123454Content-Length: 3325
6--abcde123457Content-Disposition: form-data; name="upfile"; filename="example.txt"8Content-Type: text/plain9
10File contents go here.11--abcde1234512Content-Disposition: form-data; name="note"13
14Uploading a file named "example.txt"15--abcde12345--
Multiple Upload
You can have several named file parameters, each defined individually:
1parameters:2 - in: formData3 name: upfile14 type: file5 required: true6 - in: formData7 name: upfile28 type: file9 required: false10 - in: formData11 name: upfile312 type: file13 required: false
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:
1type: array2items:3 type: string4 format: binary
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:
1curl --upload-file archive.zip http://example.com/upload
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:
1- in: formData2 name: zipfile3 type: file4 description: Contents of the ZIP file.5 x-mimetype: application/zip
Did not find what you were looking for? Ask the community
Found a mistake? Let us know