【问题标题】:Python requests does not return same result as web browserPython 请求不会返回与 Web 浏览器相同的结果
【发布时间】:2020-07-01 13:20:39
【问题描述】:

好的,这是我的问题。当我对 API 进行浏览器调用时(那个客户给了我):

https:///status.asmx/Fin?gt=14046.40&key=&cash=notes&date=2020-06-25+15%3A47%3A01&receipt_number=748&type=collect&sum=0.00&pos= 003-480

结果是:{ "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 调用,请求就可以了。不成功。 我也一直在尝试将密钥传递给请求的各种方法:

  1. 将其放入标题中 - 无效header={'key': &lt;exactly_the_same_auth_key&gt;},
  2. 将其放入身份验证 - 无效auth=(&lt;exactly_the_same_auth_key&gt;, '')

我几乎可以肯定,请求构成了 ULR 错误(因为它在浏览器中有效),但我不知道如何修复它。或者如何检查,问题出在哪里。

【问题讨论】:

  • 问题出在r = requests.post(url, data=json.dumps(event_data), timeout=5)post 的数据不是json 格式,而是url-encoded 格式,所以要么更改请求和编码方法(例如更改为@987654331 @) 和/或适当更改数据编码

标签: python api python-requests


【解决方案1】:

请求中的参数和请求的正文是有区别的。您在这里所做的是发送请求 body 中的值,但顶部的 URL 将值作为 URL 参数传递。试试这个:

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, params=event_data, timeout=5)

【讨论】:

  • 我试过了,但它返回了相同的结果:{ "1":"KEY can not be empty"}
  • 没有细节就很难说了。人们不可能在没有 URL 或可能的 API 密钥的情况下测试出了什么问题,因为这个问题是针对当前情况的。
  • 好吧,出于安全原因,我无法提供密钥和 URL。
  • 在这种情况下,您可能需要靠自己。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 2018-04-27
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 2023-02-17
相关资源
最近更新 更多