【发布时间】:2019-05-27 20:13:53
【问题描述】:
我正在尝试使用 external website 的私有 API 和 python 请求模块从他们那里购买商品。问题是,对于请求表单数据,它希望基本上我手动构建的购物车项目与购物车制作的购物车项目完全相同,据我所知,它们完全相同。当我发布请求时,我从服务器收到一条错误消息,说它没有收到任何购物车物品。
我尝试匹配标头编码(原始请求使用 application/x-www-form-urlencoded),使用请求 JSON kwarg 而不是数据,还手动将商品添加到购物车,获取它们,然后发送那些。我在想可能是我需要先对它们进行编码,但我不知道是什么。
这里有一些代码
login_data = {'email': 'email', 'password': 'password123'}
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/74.0.3729.157 Safari/537.36',
'Content-type': 'application/x-www-form-urlencoded'}
s = requests.Session()
login_response = s.post(url, data=login_data, headers=headers) # login
data_charge = {'cartItems':[cartItems], # This is a list of dicts
'key': 'licensing',
'nonce': credit_card_token, # I have to get the card token which works fine
'cardholder': 'John Smith'}
purchase_response = s.post(url_charge, data=data_charge, headers=headers)
purchase_response.json() 内容返回 "content":[{"error":"No cart items sent."}]。 它应该以订单号和所有购买的物品作为响应。
如果我遗漏了任何重要的细节,请告诉我。 谢谢!
【问题讨论】:
标签: python ajax python-3.x python-requests