Have your say in Community Polls: What was/is your greatest motivation to start your own business?

update metafield Rich Text by pyton app

update metafield Rich Text by pyton app

rootik
Visitor
1 0 0

 

I'm trying to update a rich text metafield through a Python program.

But it gives me an error: errors":{"value":["is invalid JSON: unexpected token at 'Hello World!'."]}}

Moreover, this error is issued only on the metafield field of the rich text type; if you use any other type, it works!

below is the program code

 

import requests

 

shopify_url = 'xxxxxx'

access_token = ‘xxxxxxxx’

 

def get_shopify_products():

    url = f'{shopify_url}/products.json'

    headers = {

        'X-Shopify-Access-Token': access_token

    }

 

    response = requests.get(url, headers=headers)

    if response.status_code == 200:

        return response.json()['products']

    else:

        print("Error fetching Shopify products:", response.status_code)

        return []

 

def update_shopify_product_metafield(shopify_product_id, text_value):

    url = f'{shopify_url}/products/{shopify_product_id}/metafields.json'

    headers = {

        'X-Shopify-Access-Token': access_token

    }

 

    metafield_data = {

        "metafield": {

            "namespace": "custom",

            "key": "short_description_2", 

            "value": text_value,

            "type": "rich_text_field" 

        }

    }

 

    response = requests.post(url, json=metafield_data, headers=headers)

 

    if response.status_code == 201:

        print(f"Successfully updated short description for product ID {shopify_product_id}")

    else:

        print(f"Failed to update product ID {shopify_product_id}: {response.status_code}, Response: {response.text}")

 

 

 

def main():

    

    products = get_shopify_products()

 

    

    for product in products[:1]:

        shopify_product_id = product['id']

        product_name = product['title']

        print(f"Processing product '{product_name}' with ID {shopify_product_id}")

     

        text_value=‘Hello World!’

      

        update_shopify_product_metafield(shopify_product_id, text_value)

 

 

if __name__ == "__main__":

    main()

Replies 0 (0)