Skip to content

Commit d8f0140

Browse files
committed
Reorganize multipart/form-data guidance
This organizes and streamlines the guidance on multipart that was incorporated from the Media Type Object. Lots of duplication has been removed, and the examples reworked to show distinct use cases.
1 parent 8cdbf83 commit d8f0140

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

versions/3.0.4.md

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,15 +1731,7 @@ name=example&icon=iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVQIW2P8z8
17311731

17321732
##### Encoding `multipart` Media Types
17331733

1734-
It is common to use `multipart/form-data` as a `Content-Type` when transferring request bodies to operations. In contrast to 2.0, a `schema` is REQUIRED to define the input parameters to the operation when using `multipart` content. This supports complex structures as well as supporting mechanisms for multiple file uploads.
1735-
1736-
When passing in `multipart` types, boundaries MAY be used to separate sections of the content being transferred — thus, the following default `Content-Type`s are defined for `multipart`:
1737-
1738-
* If the property is a primitive, or an array of primitive values, the default Content-Type is `text/plain`
1739-
* If the property is complex, or an array of complex values, the default Content-Type is `application/json`
1740-
* If the property is a `type: string` with `format: binary` or `format: byte` (aka a file object), the default Content-Type is `application/octet-stream`
1741-
1742-
Note that only `multipart/*` media types with named parts can be described as shown here. Note also that while `multipart/form-data` originally defined a per-part `Content-Transfer-Encoding` header that could indicate base64 encoding (`format: byte`), it has been deprecated for use with HTTP as of [RFC7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.7).
1734+
It is common to use `multipart/form-data` as a `Content-Type` when transferring forms as request bodies. In contrast to 2.0, a `schema` is REQUIRED to define the input parameters to the operation when using `multipart` content. This supports complex structures as well as supporting mechanisms for multiple file uploads.
17431735

17441736
The `form-data` disposition and its `name` parameter are mandatory for `multipart/form-data` ([RFC7578 §4.2](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.2)).
17451737
Array properties are handled by applying the same `name` to multiple parts, as is recommended by [RFC7578 §4.3](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.3) for supplying multiple values per form field.
@@ -1758,7 +1750,7 @@ See [Appendix E](#percentEncodingAndFormMediaTypes) for a detailed examination o
17581750

17591751
###### Example: Basic Multipart Form
17601752

1761-
Examples:
1753+
When the `encoding` attribute is _not_ used, the encoding is determined by the Encoding Object's defaults:
17621754

17631755
```yaml
17641756
requestBody:
@@ -1768,31 +1760,25 @@ requestBody:
17681760
type: object
17691761
properties:
17701762
id:
1763+
# default for primitives without a special format is text/plain
17711764
type: string
17721765
format: uuid
1773-
address:
1774-
# default Content-Type for objects is `application/json`
1775-
type: object
1776-
properties: {}
17771766
profileImage:
1778-
# default Content-Type for string/binary is `application/octet-stream`
1767+
# default for string with binary format is `application/octet-stream`
17791768
type: string
17801769
format: binary
1781-
children:
1782-
# default Content-Type for arrays is based on the `inner` type (text/plain here)
1783-
type: array
1784-
items:
1785-
type: string
17861770
addresses:
1787-
# default Content-Type for arrays is based on the `inner` type (object shown, so `application/json` in this example)
1771+
# default for arrays is based on the type in the `items`
1772+
# subschema, which is an object, so `application/json`
17881773
type: array
17891774
items:
17901775
$ref: '#/components/schemas/Address'
17911776
```
17921777
17931778
###### Example: Multipart Form with Encoding Objects
17941779
1795-
`multipart/form-data` allows for binary parts:
1780+
Using `encoding`, we can set more specific types for binary data, or non-JSON formats for complex values.
1781+
We can also describe headers for each part:
17961782

17971783
```yaml
17981784
requestBody:
@@ -1802,28 +1788,30 @@ requestBody:
18021788
type: object
18031789
properties:
18041790
id:
1805-
# default is text/plain
1791+
# default is `text/plain`
18061792
type: string
18071793
format: uuid
1808-
address:
1809-
# default is application/json
1810-
type: object
1811-
properties: {}
1812-
historyMetadata:
1813-
# need to declare XML format!
1814-
description: metadata in XML format
1815-
type: object
1816-
properties: {}
1794+
addresses:
1795+
# default based on the `items` subschema would be
1796+
# `application/json`, but we want these address objects
1797+
# serialized as `application/xml` instead
1798+
description: addresses in XML format
1799+
type: array
1800+
items:
1801+
$ref: '#/components/schemas/Address'
18171802
profileImage:
1818-
# default is application/octet-stream, need to declare an image type only!
1803+
# default is application/octet-stream, but we can declare
1804+
# a more specific image type or types
18191805
type: string
18201806
format: binary
18211807
encoding:
1822-
historyMetadata:
1808+
addresses:
18231809
# require XML Content-Type in utf-8 encoding
1810+
# This is applied to each address part corresponding
1811+
# to each address in he array
18241812
contentType: application/xml; charset=utf-8
18251813
profileImage:
1826-
# only accept png/jpeg
1814+
# only accept png or jpeg
18271815
contentType: image/png, image/jpeg
18281816
headers:
18291817
X-Rate-Limit-Limit:
@@ -1834,7 +1822,7 @@ requestBody:
18341822
18351823
###### Example: Multipart Form with Multiple Files
18361824
1837-
To upload multiple files, a `multipart` media type MUST be used:
1825+
In accordance with [RFC7578 §4.3](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.3), multiple files for a single form field are uploaded using the same name (`file` in this example) for each file's part:
18381826

18391827
```yaml
18401828
requestBody:

0 commit comments

Comments
 (0)