Python POST 编码和格式化

Python POST encoding and formatting

我正在使用带有 Python 的 urllib 发送一个 POST 请求。

我使用 Charles 查看通常会发送的 POST,现在我正尝试使用 urllib 发送相同的内容。

根据charles的说法应该是这样发送的:

{"stuff_values":{"description":"This is description","somestuff":[{"thing":"hello","title" :"i love cookies"}]}}

这是我要发送的:

stuff_values=%7B%27description%27%3A+%27This+is+description%27%2C+%27somestuff%27%3A+%5B%7B%27thing%27%3A+%27hello%27% 2C+%27title%27%3A+%27i+love+cookies%27%7D%5D%7D

这是我的部分代码:

values = {
"stuff_values":{
"description":"this is description",
"somestuff" : [{"thing":"hello",
"title":"i love cookies"}]}
}

data = urllib.parse.urlencode(values)
data = data.encode('ascii')
req = urllib.request.Request(url, data, headers = headers)

有人可以帮我解决这个问题吗?我猜我的编码部分有问题,但也有正确格式化其余部分的问题。

感谢您的宝贵时间!

尝试:

data = str(values)

相反。 urlencode 正在将某些字符转换为 %xx 转义字符。