如何处理 python 请求中的特殊字符 post 请求

How to handle special characters in python requests post request

我正在尝试使用 python requests 包发送 HTTP POST 请求。

有效的 curl 命令如下所示(从 chrome 开发工具网络选项卡中捕获,右键单击 someFile.php,然后选择“复制为 cURL”)。当 运行 在终端中时,它输出一个有效的、非空的响应。

curl 'https://somedomain.com/someFile.php' \
  --data-raw $'abc=ZXC%20*%20QWE%20***%20BNM%20((someThing%20%3D%200))%20AND%20anotherThing%20%3E%3D%20\'2020-5-9\'%20IOP%20&someparam=1&myhash=a5d96895cab824fbd9bb85627a8f909d'

我试图在 python 中复制 POST 请求:

import requests
url = 'https://somedomain.com/someFile.php'
out = requests.post(url,data=r'abc=ZXC%20*%20QWE%20***%20BNM%20((someThing%20%3D%200))%20AND%20anotherThing%20%3E%3D%20\'2020-5-9\'%20IOP%20&someparam=1&myhash=a5d96895cab824fbd9bb85627a8f909d')
print(out.text)

...但这只会打印一个空字符串。

如何在 python 中处理这个 curl 命令?

url="https://somedomain.com/someFile.php"
params = {"abc":"text without quote symbols", "someParam":.... }
res=requests.post(url, params=params)
print(res.text) 

只需将 Content-Type header 设置为 application/x-www-form-urlencoded 即可解决您的问题。

headers={}
headers["Content-Type"]="application/x-www-form-urlencoded"
data="....."
output=requests.post(url,headers=headers,data=data)