将multipart/form-data用作将请求主体传输到操作的Content-Type很常见。与2.0相比,当使用multipart内容时,REQUIRED使用schema来定义操作的输入参数。这支持复杂结构以及支持多种文件上传的机制。
在传递multipart类型时,可以使用边界MAY来分隔正在传输的内容部分——因此,为multipart定义了以下默认Content-Type
- 如果属性是原始类型,或原始值的数组,则默认 Content-Type 为
text/plain - 如果属性是复杂类型,或复杂值的数组,则默认 Content-Type 为
application/json - 如果属性是
type: string且format: binary或format: base64(也称为文件对象),则默认 Content-Type 为application/octet-stream
示例
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
id:
type: string
format: uuid
address:
# default Content-Type for objects is `application/json`
type: object
properties: {}
profileImage:
# default Content-Type for string/binary is `application/octet-stream`
type: string
format: binary
children:
# default Content-Type for arrays is based on the `inner` type (text/plain here)
type: array
items:
type: string
addresses:
# default Content-Type for arrays is based on the `inner` type (object shown, so `application/json` in this example)
type: array
items:
type: '#/components/schemas/Address'
引入了一个encoding属性,用于控制multipart请求主体部分的序列化。此属性仅适用于multipart和application/x-www-form-urlencoded请求主体。