Admin REST API not saving metafield

aman1234
Shopify Partner
1 0 0

 

 

def save_review_to_shopify(shopify_store, api_version, access_token, product_id, review):
    key_val = str(uuid4())
    # Define the URL and the data to be sent
    url = f"https://{shopify_store}.com/admin/api/{api_version}/products/{product_id}/metafields.json"
    data = {
            "metafield": {
                "namespace":"prapp-pub-reviews",
                "key": key_val,
                "value": review,
                "type": "json"
            }
        }
    # print("data: ", data)
    headers = {
        "X-Shopify-Access-Token": access_token,
        "Content-Type": "application/json"
    }
    response = requests.post(url, json=json.dumps(data), headers=headers)

 

 

I'm using the REST Admin API to save metafield for reviews to a product listing https://shopify.dev/docs/api/admin-rest/2024-01/resources/metafield#post-blogs-blog-id-metafields . When I run this the metafield doesn't save as it should, I get a 200 response, and all metafields associated with that product are returned. I would expect a 201 created response and for the metafield to be saved. How can I fix this issue and save the metafield?  Thanks

Replies 0 (0)