【发布时间】:2019-08-12 08:56:09
【问题描述】:
我正在尝试在 python 请求中转换此 curl 命令,但我不确定应该如何传递数据:
curl -X POST -u "apikey:{apikey}" \
--header "Content-Type: text/plain" \
--data "some plain text data" \
"{url}"
我尝试直接传递字符串并使用 str.encode('utf-8') 对其进行编码,但我收到错误 415 Unsupported Media Type
这是我的代码:
text = "some random text"
resp = requests.post(url, data=text, headers={'Content-Type': 'text/plain'}, auth=('apikey', self.apikey))
[编辑]:解决方案是不在请求中指定标头
非常感谢!
【问题讨论】:
-
通常情况下,
import requests text = "some random text" resp = requests.requests("POST",url, data=text.encode('utf-8'), headers={'Content-Type': 'text/plain'}, auth=('apikey', self.apikey))应该可以工作。但这通常取决于目标。如果您提供要发布此数据的目标,也许我们可以直接尝试 -
不要在
requests中手动设置headers像Content-Type -
谢谢@IvanVinogradov,虽然我仍然对设置标题不起作用感到惊讶,但这就像一个魅力
-
@IvanVinogradov,你能把这个作为答案发布吗? (最好有简短的解释,但有答案总比没有好)
-
好的,我会发帖
标签: python python-3.x http python-requests