简体   繁体   中英

Upload image to Shopify using Python and REST API

I have read through the Shopify API documentation for how to insert an image to Shopify from here: https://shopify.dev/docs/admin-api/rest/reference/products/product-image

I have the following code, which can successfully connect to Shopify and retrieve images associated with a product:

import requests

# Connect to Shopify
shop_url = "https://%s:%s@store.myshopify.com/admin/api/%s/products/5161320611973/images.json" % (API_KEY, PASSWORD, API_VERSION)

# Get data
data = requests.get(shop_url)

This works successfully, Now I would like to upload a new image to a product. This is the code I am using:

# Create image data
imgdata = {
  "image": {
    "src": "https://pkmncards.com/wp-content/uploads/en_US-CP-079-charizard_v.jpg"
  }
}

# POST image data to Shopify
x = requests.post(shop_url, data = imgdata)

This gives the following result:

<Response [400]>
{'errors': {'image': 'expected String to be a Hash'}}

Does anyone have a suggestion as to what mistake I may have made?

Using json instead of data in the requests should do the trick:

# POST image data to Shopify
x = requests.post(shop_url, json = imgdata)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM