ハウツーガイド - マルチパートフォーム
マルチパートフォームリクエストは通常、大きなファイルやデータをサーバーに送信するために使用されます。
このリクエスト形式は、任意のキー(execute、acquire、fieldsなど)で宣言できますカスタムアダプターコード内で使用します。これは、Content-Typeヘッダー内にデータ形式を埋め込むことで行います。
サンプルコードスニペット
例として、IBM Watson APIのConvert documentエンドポイントを使用します。このエンドポイントは、multipart/form-data形式のドキュメントを受け入れます。
cURLの例は次のようになります:
sh
curl \
https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15 \
-X POST \
-u "{username}":"{password}" \
-F config="{\"conversion_target\":\"answer_units\"}" \
-F "[email protected];type=application/pdf"Workato:
ruby
{
title: "IBM Watson",
connection: {
# Some code here
},
test: {
# Some code here
},
actions: {
upload_file: {
input_fields: lambda do
[
{ name: "file_name", type: "string" },
{ name: "file_data", type: "string" },
{ name: "conversion_target", type: "string" }
]
end,
execute: lambda do |connection, input|
post("https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document").
params(version: "2015-12-15").
request_format_multipart_form.
payload(file: [input['file_data'], 'application/pdf'],
file_name: input['file_name'],
config: "{\"conversion_target\":\"#{input['conversion_target']}\"}")
end
},
output_fields: {
# Some code here
}
},
triggers: {
# Some code here
},
object_definitions: {
# Some code here
},
pick_lists: {
# Some code here
},
methods: {
# Some code here
}SDKでは、payload内のfileキーが長さ2の配列を取ることに注意してください。これにより、リクエストがフォームデータとして定義されます。配列の最初の項目はファイルデータで、2番目の項目は入力ファイルのメディアタイプ(MIMEタイプ)です。
コンポーネント
| cURL | Workato |
|---|---|
curl https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15 -X POST | post("https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document").params(version: "2015-12-15") |
-u "{username}":"{password}" | これは[コネクション](../authentication/basic-authentication.md)キーで定義され、送信リクエストに自動的に追加されます。 |
-F config="{\"conversion_target\":\"answer_units\"}"-F "[email protected];type=application/pdf" | .request_format_multipart_form.payload(file: [input['file_data'], 'application/pdf'], file_name: input['file_name'],config: "{\"conversion_target\":\"#{input['conversion_target']}\"}") |
バリエーション
場合によっては、前の例のような別個のキーと値のペアではなく、ファイルペイロードの一部として、マルチパートフォーム内でファイル名を明示的に指定する必要があります。これに対応するには、ペイロードを次のように調整できます。
FILE_NAMEはFILEとは異なります
以下の例のfile_nameは、ペイロードキー(file)とは異なります。
ruby
execute: lambda do |connection, input|
post("https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document").
params(version: "2015-12-15").
request_format_multipart_form.
payload(file: [input['file_data'], 'application/pdf', input['file_name']],
config: "{\"conversion_target\":\"#{input['conversion_target']}\"}")
end最終更新日: