Multipart Requests
Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object). In OpenAPI 3, you describe a multipart request in the following way:
1requestBody:2  content:3    multipart/form-data: # Media type4      schema: # Request payload5        type: object6        properties: # Request parts7          id: # Part 1 (string value)8            type: string9            format: uuid10          address: # Part2 (object)11            type: object12            properties:13              street:14                type: string15              city:16                type: string17          profileImage: # Part 3 (an image)18            type: string19            format: binaryYou start with the requestBody/content keyword. Then, you specify the media type of request data. File uploads typically use the _multipart/form-data_ media type, and mixed-data requests usually use _multipart/mixed_. Below the media type, put the schema keyword to indicate that you start describing the request payload. You describe individual parts of the request as properties of the schema object. As you can see, a multipart request can include various data: strings, objects in JSON format, and binary data. You can also specify one or several files for uploading. (To learn more, see File Upload.) The example above corresponds to the following request:
1POST /upload HTTP/1.12Content-Length: 4283Content-Type: multipart/form-data; boundary=abcde123454
5--abcde123456Content-Disposition: form-data; name="id"7Content-Type: text/plain8
9123e4567-e89b-12d3-a456-42665544000010--abcde1234511Content-Disposition: form-data; name="address"12Content-Type: application/json13
14{15  "street": "3, Garden St",16  "city": "Hillsbery, UT"17}18--abcde1234519Content-Disposition: form-data; name="profileImage "; filename="image1.png"20Content-Type: application/octet-stream21
22{…file content…}23--abcde12345--Specifying Content-Type
By default, the Content-Type of individual request parts is set automatically according to the type of the schema properties that describe the request parts:
Schema Property Type
Content-Type
Primitive or array of primitives
text/plain
Complex value or array of complex values
application/json
String in the binary or base64 format
application/octet-stream
To set a specific Content-Type for a request part, use the encoding/_{property-name}_/contentType field. You add encoding as a child of the media type property, one the same level where schema is located. In the example below, we set the contentType for the profileImage part of a multipart request to image/png, image/jpg:
1requestBody:2  content:3    multipart/form-data:4      schema:5        type: object6        properties: # Request parts7          id:8            type: string9            format: uuid10          address:11            type: object12            properties:13              street:14                type: string15              city:16                type: string17          profileImage:18            type: string19            format: base6420      encoding: # The same level as schema21        profileImage: # Property name (see above)22          contentType: image/png, image/jpegSpecifying Custom Headers
Parts of multipart requests usually do not use any headers, except for Content. In case you need to include custom headers, use the encoding/_{property-name}_/headers field to describe these headers (see below). For complete information on describing headers, see Describing Parameters. Below is an example of a custom header defined for a part of a multipart request:
1requestBody:2  content:3    multipart/form-data:4      schema:5        type: object6        properties:7          id:8            type: string9            format: uuid10          profileImage:11            type: string12            format: binary13      encoding:14        profileImage: # Property name15          contentType: image/png, image/jpeg16          headers: # Custom headers17            X-Custom-Header:18              description: This is a custom header19              schema:20                type: stringThis declaration matches the following request:
1POST /upload HTTP/1.12Content-Length: 4283Content-Type: multipart/form-data; boundary=abcde123454
5--abcde123456Content-Disposition: form-data; name="id"7Content-Type: text/plain8
9123e4567-e89b-12d3-a456-42665544000010--abcde1234511Content-Disposition: form-data; name="profileImage"; filename="image1.png"12Content-Type: image/png13X-Custom-Header: x-header14
15{…file content…}16--abcde12345--Did not find what you were looking for? Ask the community
Found a mistake? Let us know