首页 > 解决方案 > 如何发送没有'u'字符的发布请求?[蟒蛇请求]

问题描述

我想将发布请求发送到某个应用程序。
此代码不适用于西里尔字符串(用户使用错误字符保存):

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

标签: pythonjsonunicodepython-requestscyrillic

解决方案


推荐阅读