Uploading Item Image using Square Connect API

喜欢而已 提交于 2019-12-24 10:38:45

问题


I've reviewed the examples posted in the Square Connect API documentation and the examples on GitHub, however, I can't seem to adapt these examples to the guidance on uploading images: http://docs.connect.squareup.com/#post-image

Part of the challenge is working with the Content-Type: multipart/form-data which only the image upload requires so the documentation is non-existent (with the connect-api docs).

My ultimate question is, can Square please post an example of how to upload images? Most relevant would be an example that shows how to update multiple items with images versus just one item. Any help is appreciated.


回答1:


Thanks for pointing out this gap in the documentation. The function below uses the Requests Python library to upload an image for an item (this library makes multipart/form-data requests significantly simpler). Note that you'll need to install Requests first if you haven't.

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 is the ID of the item you're uploading an image for.
  • image_path is the relative path to the image you're uploading.
  • access_token is the access token for the merchant you're acting on behalf of.

It isn't possible to upload images for multiple items in a single request to this endpoint. Instead, send a separate request for each item.



来源:https://stackoverflow.com/questions/27978978/uploading-item-image-using-square-connect-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!