You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reorganize multipart/form-data guidance (3.1.1 port of 3923 6/6, 3929)
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.
Copy file name to clipboardExpand all lines: versions/3.1.1.md
+34-47Lines changed: 34 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -1805,19 +1805,7 @@ However, this is not guaranteed, so it may be more interoperable to keep the pad
1805
1805
1806
1806
##### Encoding `multipart` Media Types
1807
1807
1808
-
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.
1809
-
1810
-
In a `multipart/form-data` request body, each schema property, or each element of a schema array property, takes a section in the payload with an internal header as defined by [RFC7578](https://tools.ietf.org/html/rfc7578). The serialization strategy for each property of a `multipart/form-data` request body can be specified in an associated [`Encoding Object`](#encodingObject).
1811
-
1812
-
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`:
1813
-
1814
-
* If the property is a primitive, or an array of primitive values, the default Content-Type is `text/plain`
1815
-
* If the property is complex, or an array of complex values, the default Content-Type is `application/json`
1816
-
* If the property is a `type: string` with a `contentEncoding`, the default Content-Type is `application/octet-stream`
1817
-
1818
-
Per the JSON Schema specification, `contentMediaType` without `contentEncoding` present is treated as if `contentEncoding: identity` were present. While useful for embedding text documents such as `text/html` into JSON strings, it is not useful for a `multipart/form-data` part, as it just causes the document to be treated as `text/plain` instead of its actual media type. Use the Encoding Object without `contentMediaType` if no `contentEncoding` is required.
1819
-
1820
-
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 (`contentEncoding: base64`), it has been deprecated for use with HTTP as of [RFC7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.7).
1808
+
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.
1821
1809
1822
1810
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)).
1823
1811
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.
@@ -1829,14 +1817,18 @@ It is not currently possible to correlate schema properties with unnamed, ordere
1829
1817
Note that there are significant restrictions on what headers can be used with `multipart` media types in general ([RFC2046 §5.1](https://www.rfc-editor.org/rfc/rfc2046.html#section-5.1)) and `multi-part/form-data` in particular ([RFC7578 §4.8](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.8)).
1830
1818
1831
1819
Note also that `Content-Transfer-Encoding` is deprecated for `multipart/form-data` ([RFC7578 §4.7](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.7)) where binary data is supported, as it is in HTTP.
1832
-
Using `contentEncoding` is equivalent to setting `Content-Transfer-Encoding` to the same value.
1833
-
If `contentEncoding` is used along with setting a different `Content-Transfer-Encoding` value with the `headers` field, the result is undefined.
1820
+
1821
+
+Using `contentEncoding` for a multipart field is equivalent to specifying an [Encoding Object](#encodingObject) with a `headers` field containing `Content-Transfer-Encoding` with a schema that requires the value used in `contentEncoding`.
1822
+
+If `contentEncoding` is used for a multipart field that has an Encoding Object with a `headers` field containing `Content-Transfer-Encoding` with a schema that disallows the value from `contentEncoding`, the result is undefined for serialization and parsing.
1823
+
1824
+
Note that as stated in [Working with Binary Data](#binaryData), if the Encoding Object's `contentType`, whether set explicitly or implicitly through its default value rules, disagrees with the `contentMediaType` in a Schema Object, the `contentMediaType` SHALL be ignored.
1825
+
Because of this, and because the Encoding Object's `contentType` defaulting rules do not take the Schema Object's` contentMediaType` into account, the use of `contentMediaType` with an Encoding Object is NOT RECOMMENDED.
1834
1826
1835
1827
See [Appendix E](#percentEncodingAndFormMediaTypes) for a detailed examination of percent-encoding concerns for form media types.
1836
1828
1837
1829
###### Example: Basic Multipart Form
1838
1830
1839
-
Examples:
1831
+
When the `encoding` attribute is _not_ used, the encoding is determined by the Encoding Object's defaults:
1840
1832
1841
1833
```yaml
1842
1834
requestBody:
@@ -1846,27 +1838,16 @@ requestBody:
1846
1838
type: object
1847
1839
properties:
1848
1840
id:
1841
+
# default for primitives without a special format is text/plain
1849
1842
type: string
1850
1843
format: uuid
1851
-
address:
1852
-
# default Content-Type for objects is `application/json`
1853
-
type: object
1854
-
properties: {}
1855
1844
profileImage:
1856
-
# default Content-Type for properties with type string and a contentEncoding
1857
-
# is `application/octet-stream`, so `image/png` must be set using contentMediaType
1845
+
# default for string with binary format is `application/octet-stream`
1858
1846
type: string
1859
-
contentMediaType: image/png
1860
-
contentEncoding: base64
1861
-
children:
1862
-
# default Content-Type for arrays is based on the items subschema type, which
1863
-
# is a string, producing a default of `text/plain`
1864
-
type: array
1865
-
items:
1866
-
type: string
1847
+
format: binary
1867
1848
addresses:
1868
-
# default Content-Type for arrays is based on the items subschema type, which
1869
-
# is an object, producing a default of `application/json`
1849
+
# default for arrays is based on the type in the `items`
1850
+
#subschema, which is an object, so `application/json`
1870
1851
type: array
1871
1852
items:
1872
1853
type: object
@@ -1875,7 +1856,8 @@ requestBody:
1875
1856
1876
1857
###### Example: Multipart Form with Encoding Objects
1877
1858
1878
-
`multipart/form-data` allows for binary parts:
1859
+
Using `encoding`, we can set more specific types for binary data, or non-JSON formats for complex values.
1860
+
We can also describe headers for each part:
1879
1861
1880
1862
```yaml
1881
1863
requestBody:
@@ -1885,25 +1867,30 @@ requestBody:
1885
1867
type: object
1886
1868
properties:
1887
1869
id:
1888
-
# default is text/plain
1870
+
# default is `text/plain`
1889
1871
type: string
1890
1872
format: uuid
1891
-
address:
1892
-
# default is application/json
1893
-
type: object
1894
-
properties: {}
1895
-
historyMetadata:
1896
-
# need to declare XML format!
1897
-
description: metadata in XML format
1898
-
type: object
1899
-
properties: {}
1900
-
profileImage: {}
1873
+
addresses:
1874
+
# default based on the `items` subschema would be
1875
+
# `application/json`, but we want these address objects
1876
+
# serialized as `application/xml` instead
1877
+
description: addresses in XML format
1878
+
type: array
1879
+
items:
1880
+
$ref: '#/components/schemas/Address'
1881
+
profileImage:
1882
+
# default is application/octet-stream, but we can declare
1883
+
# a more specific image type or types
1884
+
type: string
1885
+
format: binary
1901
1886
encoding:
1902
-
historyMetadata:
1887
+
addresses:
1903
1888
# require XML Content-Type in utf-8 encoding
1889
+
# This is applied to each address part corresponding
1890
+
# to each address in he array
1904
1891
contentType: application/xml; charset=utf-8
1905
1892
profileImage:
1906
-
# only accept png/jpeg
1893
+
# only accept png or jpeg
1907
1894
contentType: image/png, image/jpeg
1908
1895
headers:
1909
1896
X-Rate-Limit-Limit:
@@ -1914,7 +1901,7 @@ requestBody:
1914
1901
1915
1902
###### Example: Multipart Form with Multiple Files
1916
1903
1917
-
To upload multiple files, a `multipart` media type MUST be used:
1904
+
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:
0 commit comments