我想向某些应用发送发帖请求。
此代码不适用于西里尔字母(用户保存了错误字符):
import requests
s = requests.Session()
d = {
'name': 'Вова',
'surname': 'Петров',
}
response = s.post('http://localhost:8899/app/user/', json=d)
print(response.status_code)
print(response.text)
我运行netcat
来检查原始请求:
nc -kl 8899
POST /accsrv/http/person/ HTTP/1.1
Host: localhost:8899
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 147
Content-Type: application/json
{"name": "\u0420\u2019\u0420\u0455\u0420\u0406\u0420\u00b0", "surname": "\u0420\u045f\u0420\u00b5\u0421\u201a\u0421\u0402\u0420\u0455\u0420\u0406"}
我还使用curl
进行了检查(效果很好):
curl -d '{"name":"Вова", "surname":"Петров"}' -H "Content-Type: application/json" -X POST http://localhost:8899/app/user/
POST /app/user/ HTTP/1.1
Host: localhost:8899
User-Agent: curl/7.64.0
Accept: */*
Content-Type: application/json
Content-Length: 45
{"name":"Вова", "surname":"Петров"}
所以我认为unicode编码存在问题(服务器应用程序文档薄弱,我没有访问源)。
如何将请求转为第二种形式?
Python 3.7.3