文件上传
在OpenAPI 3.0中,您可以描述随请求内容直接上传的文件以及通过multipart请求上传的文件。使用requestBody关键词来描述包含文件的请求体。在content下,指定请求媒体类型(例如image/png或application/octet-stream)。文件使用type: string模式,格式为format: binary或format: base64,具体取决于文件内容的编码方式。例如
1requestBody:2 content:3 image/png:4 schema:5 type: string6 format: binary此定义对应如下所示的HTTP请求
1POST /upload2Host: example.com3Content-Length: 8084Content-Type: image/png5
6[file content goes there]通过Multipart请求上传
要描述随其他数据一起发送的文件,请使用multipart媒体类型。例如
1requestBody:2 content:3 multipart/form-data:4 schema:5 type: object6 properties:7 orderId:8 type: integer9 userId:10 type: integer11 fileName:12 type: string13 format: binary相应的HTTP请求负载将包含多个部分
1POST /upload2Host: example.com3Content-Length: 27404Content-Type: multipart/form-data; boundary=abcde123455
6--abcde123457Content-Disposition: form-data; name="orderId"8
9119510--abcde1234511Content-Disposition: form-data; name="userId"12
1354514--abcde1234515Content-Disposition: form-data; name="fileName"; filename="attachment.txt"16Content-Type: text/plain17
18[file content goes there]19--abcde12345--多文件上传
使用multipart媒体类型来定义上传任意数量的文件(文件数组)
1requestBody:2 content:3 multipart/form-data:4 schema:5 type: object6 properties:7 filename:8 type: array9 items:10 type: string11 format: binary相应的HTTP请求将如下所示
1POST /upload2Host: example.com3Content-Length: 27404Content-Type: multipart/form-data; boundary=abcde123455
6--abcde123457Content-Disposition: form-data; name="fileName"; filename="file1.txt"8Content-Type: text/plain9
10[file content goes there]11--abcde1234512Content-Disposition: form-data; name="fileName"; filename="file2.png"13Content-Type: image/png14
15[file content goes there]16--abcde1234517Content-Disposition: form-data; name="fileName"; filename="file3.jpg"18Content-Type: image/jpeg19
20[file content goes there]21--abcde12345--参考
有关OpenAPI中文件上传的更多信息,请参阅OpenAPI 3.0规范的以下部分