2

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.

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
justanotherguy
  • 395
  • 4
  • 17
  • You might be missing a session cookie, that will tie your requests together. Your last `GET` request needs to be recognized as belonging to the same session as the `POST` request that added the item to the cart. – JSTL Jul 30 '18 at 19:46
  • See this [question and answer](https://stackoverflow.com/questions/25091976/python-requests-get-cookies) for how you can create a session and use cookies in requests. – JSTL Jul 30 '18 at 19:52
  • Wow you guys are life savers, I just tried it and it seems to be working. Thanks – justanotherguy Jul 30 '18 at 19:56

1 Answers1

2

You perform the correct POST/GET call, however you need to take into account the fact that you also need some way to track your "session". Likely on a real page, cookies are used to track the contents of your cart. As a result, when you request your cart contents, you will need to include this cookie. To do so, add cookies to your code using a requests session:

s = requests.Session() # cookies are stored in the session

payload = {'action': 'add', 'product_id': 4806, 'qty[]': 1}

get = s.get("https://www.toytokyo.com/medicom-toy-kaws-together-black/")

post = s.post("https://www.toytokyo.com/remote/v1/cart/add", data=payload)

print(post.status_code, post.content)

get = s.get("https://www.toytokyo.com/cart.php")

print(get.status_code, get.text)
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
  • You were right. I have one more question, there are 2 different ways that I have seen sessions work. there is the request.Session() and the request.sessions . What is the difference? – justanotherguy Jul 30 '18 at 19:58
  • @shayang these are two different types, request.sessions is a module, and request.Session() is an instance of a requests.sessions.Session: `>>> requests.sessions >>> requests.Session() ` – TheoretiCAL Jul 30 '18 at 21:43
  • when do I use headers? I've seen some people use them, and some dont. Is it needed? – justanotherguy Jul 31 '18 at 00:05
  • Its optional, it really depends on the server/api your talking to. Some REST apis require any json data to be posted with the header 'Content-type:application/json' or else the data will not be processed. – TheoretiCAL Jul 31 '18 at 00:53