Shopify: Register products with HTML description using GraphQL

Shopify: Register products with HTML description using GraphQL


0

I’m new to shopify GraphQL and trying to register product through GraphQL and Python.

I write a code as below but it returns error. Products with only text can be registerd but product with HTML not registerd.

■Using GCP,Theme:Debut


import requests
import json

# Shopify API credentials
shop_url = "ShopURL"
access_token = "ACCESS TOKEN"


# GraphQL mutation
mutation = '''
mutation {
  productCreate(input: {
    title: "ProductCreateTest"
    descriptionHtml: "<img data-src="https://cdn.shopify.com/s/files/test.jpg?v=123456" alt=""><br><br><p>"doublequotes"</p>"
    variants: [
      {
        price: 29.99
      }
    ]
    images: [
      {
        src: "https://example.com/image.jpg"
      }
    ]
  }) {
    product {
      id
      title
    }
  }
}
'''

# GraphQL API endpoint
api_url = f"{shop_url}/admin/api/2023-07/graphql.json"

# Headers with authentication
headers = {
    "Content-Type": "application/json",
    "X-Shopify-Access-Token": access_token
}

# Send the GraphQL request
response = requests.post(api_url, headers=headers, data=json.dumps({"query": mutation}))

# Check for success or handle errors
if response.status_code == 200:
    result = response.json()
    created_product = result.get("data", {}).get("productCreate", {}).get("product", {})
    if created_product:
        product_id = created_product.get("id")
        print(f"Product created with ID: {product_id}")
    else:
        print("Error creating product")
else:
    print(f"Error: {response.status_code}, {response.text}")

When putting plain text on descriptionHTML field, it works perfectly.

So, I’m considering escape doesn’t work on html.


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *