Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Updating Product with API

Solved

Updating Product with API

DJ-Qst
Visitor
2 1 0

Howdy everybody, I'm developing an app for work, and I've reached a point where I'm stuck. Here's the code and then I'll elaborate:

 

product_id = updatedItem['item']['id']
title = updatedItem['item']['handle']
payload = {"product": {"id": product_id, "title": title}}
posted = requests.put(f"{self.shopifyLink}products/{product_id}.json", data=payload)

 

 In this, the updatedItem is a pre stored variable with some data in it, and the item and id tags access the product's ID. My self.shopifyLink is good, it's the same one I've used on other calls. Regardless, this gives me an error code 400 every single time, regardless of what I try.

 

Eventually I'm going to be primarily updating the product with updated variants, but for now, I'm just trying to get it to work properly so I can see a sweet status of 200. Thank you for any help.

Accepted Solution (1)

DJ-Qst
Visitor
2 1 0

This is an accepted solution.

I have done it! It took 3 weeks but I have solved the issue . . .

 

This is the original, but slightly changed for clarity. Now showing the loop instead of having it offscreen and sending the whole product

 

for product in product_list:
     product_id = updatedItem['item']['id']
     payload = {"product": product}
     posted = requests.put(f"{self.shopifyLink}products/{product_id}.json", data=payload)

 

The issue here is that when you get a 400, that means Shopify is expecting a JSON object, as they tell you here:  https://shopify.dev/api/usage/response-codes They don't say anything about it (although they should) in the documentation, so that was the cause of my problems

 

To fix this, I had to do two things:

import json

for product in product_list:
     product_id = updatedItem['item']['id']
     payload = {"product": product}
     headers = {'content-type': 'application/json'}
     posted = requests.put(f"{self.shopifyLink}products/{product_id}.json", data=json.dumps(payload), headers=headers)

The first thing I did is to make the payload a JSON object WHEN I MAKE THE CALL, If you do it when creating the variable, it won't work (at least it didn't for me) The second thing I did is to set the headers to tell Shopify I'm giving it a JSON. 

 

If I'm reading the python documentation right, the requests module says only one of these changes is necessary as long as my payload is formatted properly, but my code didn't seem to work unless both changes were there.

 

I hope this helps!

 

View solution in original post

Reply 1 (1)

DJ-Qst
Visitor
2 1 0

This is an accepted solution.

I have done it! It took 3 weeks but I have solved the issue . . .

 

This is the original, but slightly changed for clarity. Now showing the loop instead of having it offscreen and sending the whole product

 

for product in product_list:
     product_id = updatedItem['item']['id']
     payload = {"product": product}
     posted = requests.put(f"{self.shopifyLink}products/{product_id}.json", data=payload)

 

The issue here is that when you get a 400, that means Shopify is expecting a JSON object, as they tell you here:  https://shopify.dev/api/usage/response-codes They don't say anything about it (although they should) in the documentation, so that was the cause of my problems

 

To fix this, I had to do two things:

import json

for product in product_list:
     product_id = updatedItem['item']['id']
     payload = {"product": product}
     headers = {'content-type': 'application/json'}
     posted = requests.put(f"{self.shopifyLink}products/{product_id}.json", data=json.dumps(payload), headers=headers)

The first thing I did is to make the payload a JSON object WHEN I MAKE THE CALL, If you do it when creating the variable, it won't work (at least it didn't for me) The second thing I did is to set the headers to tell Shopify I'm giving it a JSON. 

 

If I'm reading the python documentation right, the requests module says only one of these changes is necessary as long as my payload is formatted properly, but my code didn't seem to work unless both changes were there.

 

I hope this helps!