PUT response is 200, but nothing changes

Solved
Shopify Partner
5 1 0

Using the following python code I get a 200 back on my put, but the product is still published, and the published_at time is not null.  I want to be able to activate/deactivate products via an api call. 

import requests

def update_product():

    shopify_prod_id = <product_id>

    payload = '''{
      "product": {
        "published": false,
        "published_at":"null"
        "id": %d
      }
    }'''%(shopify_prod_id)

    headers = {"Content-Type": "application/json"}

    resp = requests.put('https://<SHOPIFY_API_KEY>:<SHOPIFY_API_SECRET>@<SHOPIFY_SHOP_FULL_NAME>/admin/products/%d.json'%(shopify_prod_id), json=payload, headers=headers)

    print('Product id {} update response: {}\n{}'.format(shopify_prod_id,resp.status_code,resp.content)) 
 

if __name__ == '__main__':
    update_product()

Please review, and let me know what I am doing wrong.

Thank you.

0 Likes
Shopify Staff
Shopify Staff
270 27 51

Hey there,

 

Can you please provide me with the `X-Request-Id` response header for the PUT request you're making?

 

Thanks!

0 Likes
Shopify Partner
5 1 0

Sure.

'X-Request-Id': '861e3202-77cb-49af-b9ca-ea83604610e8'

0 Likes
Shopify Partner
5 1 0

Modified the code:

import requests

def update_product():

    shopify_prod_id = product_id

    payload = '''{"product": {"id": %d, "published": false } }'''%(shopify_prod_id)

    headers = {"Content-Type": "application/json"}

    resp = requests.put('https://<key>:<pwd>@<store>/admin/api/2019-04/products/%d.json'%(shopify_prod_id), json=payload, headers=headers)
    print(resp.headers)
   
  
if __name__ == '__main__':
    update_product()

Now I am getting b'{"errors":{"product":"Required parameter missing or invalid"}}'

0 Likes
Shopify Staff
Shopify Staff
270 27 51

It looks as though the issue is with your JSON payload format.

 

We’re not able to properly parse theproduct_idand product_published values from the string you’re passing which is why the update is not happening.

 

I would double check the formatting on your payload string, remove any unnecessary spaces etc. Also, you’re passing the payload in under thejson parameter for your PUT request, should this not be under thedata parameter instead?

0 Likes
Highlighted

Success.

Shopify Partner
5 1 0

Thank you Zameer.  You are correct, it should be data, and not json.  Things are working now.  

0 Likes