I have been browsing stackoverflow for the past couple of days and have been looking at a lot of different videos and forums, but I can't get this to work for some reason. I'm trying to automatically add an item to cart on https://www.toytokyo.com/medicom-toy-kaws-together-black/ and I even get the correct 200 response code, but when check the shopping cart it says that its empty.
Here is the Request Payload that it needs.
------WebKitFormBoundary2abcTSnRV9XhBx4h
Content-Disposition: form-data; name="action"
add
------WebKitFormBoundary2abcTSnRV9XhBx4h
Content-Disposition: form-data; name="product_id"
4806
------WebKitFormBoundary2abcTSnRV9XhBx4h
Content-Disposition: form-data; name="qty[]"
1
------WebKitFormBoundary2abcTSnRV9XhBx4h--
and here is what I'm doing to send the POST request.
payload = {'action': 'add', 'product_id': 4806, 'qty[]': 1}
get = requests.get("https://www.toytokyo.com/medicom-toy-kaws-together-black/")
post = requests.post("https://www.toytokyo.com/remote/v1/cart/add", data=payload)
print(post.status_code, post.content)
get = requests.get("https://www.toytokyo.com/cart.php")
print(get.status_code, get.text)
I'm not sure if I'm doing something wrong, but I get the correct response from what I can tell.
EDIT: ANSWER BELOW
Just for anyone that might stumble across this later on, I took the advice of the people who commented below and created a variable called session
and assigned it using session = requests.Session()
which allows your program to persist across every new request that you send. the session
variable also has all of the same methods as the request itself. So I just replaced everything that used requests and replaced it with session.