Shopify: Register products with HTML description using GraphQL
Shopify: Register products with HTML description using GraphQL
I'm new to Shopify GraphQL and trying to register products through GraphQL and Python.
I wrote a code as below but it returns an error. Products with only text can be registered but products with HTML are not registered.
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 the descriptionHTML field, it works perfectly.
So, I'm considering Escape doesn't work on HTML.
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand