小编qme*_*eus的帖子

我应该如何将文本/纯数据传递给 python 的 requests.post?

我试图curl在 python 请求中转换这个命令,但我不确定我应该如何传递数据:

curl -X POST -u "apikey:{apikey}" \
    --header "Content-Type: text/plain" \
    --data "some plain text data" \
    "{url}"
Run Code Online (Sandbox Code Playgroud)

我试图直接传递字符串并对其进行编码,str.encode('utf-8')但出现错误 415Unsupported Media Type

这是我的代码:

text = "some random text"
resp = requests.post(url, data=text, headers={'Content-Type': 'text/plain'}, auth=('apikey', self.apikey))
Run Code Online (Sandbox Code Playgroud)

[编辑]:解决方案是不在请求中指定标头

非常感谢!

python http python-3.x python-requests

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

http ×1

python ×1

python-3.x ×1

python-requests ×1