python - 当同一个 POST 在 Postman 中正常工作时,如何解决 Python 请求 POST 失败的问题?

标签 python python-3.x python-requests

在发出在 Postman 中正常工作的 POST 请求时,我继续从 python 请求中收到非常通用、无用的错误消息。

无论我尝试什么,我都会继续收到两条错误消息之一。请注意,调用 python 脚本有第 155 行,也不有效负载包含字母“u”:

{"error":{"detail":"SyntaxError: Unexpected token: u (sys_script_include.d2426c9ec0a8016501958bf2ac79c775.script; line 155)","message":"Unexpected token: u"},"status":"failure"}

{"error":{"message":"Unexpected token: u","detail":"SyntaxError: Unexpected token: u (sys_script_include.d2426c9ec0a8016501958bf2ac79c775.script; line 155)"},"status":"failure"}

Postman中,参数被正确解释,然后附加到url中,例如:

https://areallylongurl?params={"catalogItem": "Req Name"}

Python请求中,我尝试了各种组合,但没有运气。

payload = {"params": '{"catalogItem": "Req Name"}'}
response = requests.post(url, headers=headers, json=payload, verify=False)
response = requests.post(url, headers=headers, json=json.dumps(payload), verify=False)
response = requests.post(url, headers=headers, data=payload, verify=False)
response = requests.post(url, headers=headers, data=json.dumps(payload), verify=False)

作者:using this very helpful SO answer ,我能够进一步分析 Requests 库如何解释我提供的有效负载,但我仍然不确定如何准确解释此通用错误消息,或者可能的原因是什么。

有人知道根本问题可能是什么吗?请注意,我可以从此 API 获取数据,而不会出现请求问题,只有 POST 存在问题。

最佳答案

由于在 postman 中,参数“附加到网址”,如 https://areallylongurl?params={"catalogItem": "Req Name"},这意味着该请求可能是GET 请求,其中 JSON 作为值传递给 params 参数,而不是传递给 POST 请求的负载,在这种情况下,您应该这样做:

response = requests.get(url, headers=headers, params={"params": json.dumps(payload)}, verify=False)

关于python - 当同一个 POST 在 Postman 中正常工作时,如何解决 Python 请求 POST 失败的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52236636/

相关文章:

Python Folium Topojson 文件未渲染

python - tensorflow : How to build efficient NLP pipeline using tf Dataset

python - 脚本头过早结束 python cgi 脚本中的错误

python - 交替合并两个列表的n个元素

python - 为什么 Python 3.5 在使用正确凭据的 MySQL 连接上崩溃?

Python POST 请求未经过登录页面

Python 发布请求更改日期

Python 伯克利 DB/Sqlite

python - 具有不同条件语句的函数返回

python - 将字节对象列表转换为字典