ドキュメントのこのギャップを指摘していただきありがとうございます。以下の関数は、Requests Python ライブラリを使用してアイテムの画像をアップロードします (このライブラリにより、multipart/form-data リクエストが大幅に簡素化されます)。まだインストールしていない場合は、最初にRequestsをインストールする必要があることに注意してください。
import requests
def upload_item_image(item_id, image_path, access_token):
endpoint_path = 'https://connect.squareup.com/v1/' + your location + '/items/' + item_id + '/image'
# Don't include a Content-Type header, because the Requests library adds its own
upload_request_headers = {'Authorization': 'Bearer ' + access_token,
'Accept': 'application/json'}
# Be sure to set the correct MIME type for the image
files = [('image_data', (image_path, open(image_path, 'rb'), "image/jpeg"))]
response = requests.post(endpoint_path, files=files, headers=upload_request_headers)
# Print the response body
print response.text
item_id
画像をアップロードするアイテムの ID です。
image_path
アップロードする画像への相対パスです。
access_token
あなたが代理しているマーチャントのアクセストークンです。
このエンドポイントへの 1 回のリクエストで複数のアイテムの画像をアップロードすることはできません。代わりに、アイテムごとに個別のリクエストを送信してください。