マルチパートリクエスト
マルチパートリクエストは、複数のデータセットを境界で区切って単一のボディに結合します。通常、これらのリクエストはファイルアップロードや、単一のリクエストで複数の種類のデータ(例:ファイルとJSONオブジェクト)を転送するために使用されます。OpenAPI 3では、マルチパートリクエストを次のように記述します。
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: binary
requestBody/content
キーワードから始めます。次に、リクエストデータのメディアタイプを指定します。ファイルアップロードでは通常_multipart/form-data_
メディアタイプを使用し、混合データリクエストでは通常_multipart/mixed_
を使用します。メディアタイプの下にschema
キーワードを置き、リクエストペイロードの記述を開始することを示します。リクエストの個々の部分は、schema
オブジェクトのプロパティとして記述します。ご覧のとおり、マルチパートリクエストには、文字列、JSON形式のオブジェクト、バイナリデータなど、さまざまなデータを含めることができます。また、アップロード用に1つまたは複数のファイルを指定することもできます。(詳細については、「ファイルアップロード」を参照してください。)上記の例は、次のリクエストに対応しています。
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--
Content-Typeの指定
デフォルトでは、個々のリクエストパートのContent-Type
は、リクエストパートを記述するschema
プロパティのタイプに応じて自動的に設定されます。
スキーマプロパティタイプ
Content-Type
プリミティブまたはプリミティブの配列
text/plain
複合値または複合値の配列
application/json
binary
またはbase64
形式の文字列
application/octet-stream
リクエストパートに特定のContent-Type
を設定するには、encoding/_{property-name}_/contentType
フィールドを使用します。encoding
は、schema
と同じレベルでメディアタイププロパティの子として追加します。以下の例では、マルチパートリクエストのprofileImage
パートのcontentType
を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/jpeg
カスタムヘッダーの指定
マルチパートリクエストのパートは通常、Content
以外のヘッダーを使用しません。カスタムヘッダーを含める必要がある場合は、encoding/_{property-name}_/headers
フィールドを使用してこれらのヘッダーを記述します(以下を参照)。ヘッダーの記述に関する完全な情報については、「パラメーターの記述」を参照してください。以下は、マルチパートリクエストの一部に定義されたカスタムヘッダーの例です。
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: string
この宣言は、次のリクエストと一致します。
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--
お探しのものが見つかりませんでしたか? コミュニティに質問する
間違いを見つけましたか? お知らせください