Im trying to create a requests python script that will add to cart and eventually checkout. I did a Post Requests to
(https://yeezysupply.com/cart/add.js) which is the add to cart endpoint I found in the networks chrome developer tools page. It has a json payload with 3 dictionaries. Id which is the variant ID from the product, properties which I don’t know what it is so I left it blank, and quantity. I entered the data as a param when I did the Post requests. I received a 400 Response Error. When I printed the requests Text, nothing was added to my cart and i received this.
{
"status":"bad_request",
"message":"expected String to be a Hash: properties",
"description":"expected String to be a Hash: properties"
}
Im pretty new to requests so I’m not sure what the error means.
I was able to confirm nothing was added to my cart because I did a get requests to the shopify cart endpoint (https://yeezysupply.com/cart.json). When I print the get requests I get this.
{
"token":"cb67e6c53c63b930b4aca1eb3b5a7510",
"note":null,
"attributes":{
},
"original_total_price":0,
"total_price":0,
"total_discount":0,
"total_weight":0.0,
"item_count":0,
"items":[
],
"requires_shipping":false,
"currency":"USD",
"items_subtotal_price":0,
"cart_level_discount_applications":[
]
}
This confirmed nothing was added to my cart. Does anyone know what I’m doing wrong? The product I used for my testing is (https://yeezysupply.com/products/flannel-lined-canvas-jacket-medium-blue?c=%2Fcollections%2Fwomen)
I’ve tried creating a global requests session to see if I needed cookies. This didn’t work either.
import requests
from bs4 import BeautifulSoup as soup
session = requests.Session()
atc_endpoint = 'https://yeezysupply.com/cart/add.js'
atc_info = {
"id": "1457089478675",
"properties": "{}",
"quantity": "1"
}
def add_to_cart():
pass
atc_post = session.post(atc_endpoint, data=atc_info)
atc_get = session.get('https://yeezysupply.com/cart.json')
print(atc_post.text)
I tried using headers, I used headers = {“Content-Type”: “application/json”}
I received the following error:
{
"error":"822: unexpected token at 'id=1457089478675u0026properties=%7B%7Du0026quantity=1'"
}
Im not sure what token the api is asking for.
I expect to have the item in my cart and shown in the get requests text.
2
Answers
Try the following things-
Add
{"Content-Type": "application/json"}
as a header to your request. It would look like this-This should do the trick. Your dictionary looks good to me but if this still gives errors, try to use
json.loads
on your dictionary before sending it.Hope this helps. 🙂
So you’re building a Bot to checkout products (it would seem anyway). No offence to your talents with Python, but your life would get absolutely better if you just used Javascript to make your bot do your bidding. Since is naturally built into browsers anyway, your efforts would be simplified.
If you wanted to run your Bot server-side with Python as your question kind of indicates, and a POST is giving you troubles, just wait till you script checkout! I am not sure you can even do that at this point, so you might want to put the brakes on your plans until you can demonstrably check out without issue. Did you look into that?