【发布时间】:2020-07-01 13:20:39
【问题描述】:
好的,这是我的问题。当我对 API 进行浏览器调用时(那个客户给了我):
https://
结果是:{ "0":"Success"}
但是,当我对 python requests 库进行相同的调用时(我希望它是相同的调用):
event_data = {'key': <exactly_the_same_auth_key>,
'pos': DEVICE_ID,
'type': 'monthlybalance',
'total': close_data['overall']['price'],
'cash': close_data['overall']['price_c'],
'cards': close_data['overall']['price_cl'],
'coinbox': sorted_cash_data['coinbox'],
'notebox': sorted_cash_data['notebox'],
'coindispenser': sorted_cash_data['coindispenser'],
'notedispenser': sorted_cash_data['notedispenser'],
'startdate': close_data['header']['from'].strftime('%Y-%m-%d %H:%M:%S'),
'enddate': close_data['header']['to'].strftime('%Y-%m-%d %H:%M:%S'),
'receipt_number': 'none',
'gt': self.get_grand_total()}
r = requests.post(url, data=json.dumps(event_data), timeout=5)
结果是:{ "1":"KEY can not be empty"}
这是他们做出的结果,但它与 WEB 调用的结果不同,很明显,API 没有正确获取密钥。
但是,我今天一整天都在尝试,想找到一种方法来打印原始 URL 调用,请求就可以了。不成功。 我也一直在尝试将密钥传递给请求的各种方法:
- 将其放入标题中 - 无效
header={'key': <exactly_the_same_auth_key>}, - 将其放入身份验证 - 无效
auth=(<exactly_the_same_auth_key>, '')
我几乎可以肯定,请求构成了 ULR 错误(因为它在浏览器中有效),但我不知道如何修复它。或者如何检查,问题出在哪里。
【问题讨论】:
-
问题出在
r = requests.post(url, data=json.dumps(event_data), timeout=5),post的数据不是json格式,而是url-encoded格式,所以要么更改请求和编码方法(例如更改为@987654331 @) 和/或适当更改数据编码
标签: python api python-requests