Python请求在POST请求中发送Content-Length 0

时间:2014-09-25 07:37:16

标签: python python-requests

真的应该是什么,还是有问题?

我正在这样做,就像在这样的文档中提到的那样:

response = requests.post(path, client_id=self.app_id, client_secret=self.app_secret, grant_type="client_credentials")

response.request.headers的输出

{'Content-Length': '0', 'User-Agent': 'python-requests/2.4.1 CPython/2.7.5 Darwin/13.3.0', 'Connection': 'keep-alive', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate'}

我在做什么错误?

1 个答案:

答案 0 :(得分:3)

您没有发送任何POST正文,因此内容长度为0.标题完全正确。如果您要发送POST正文,请设置data关键字参数,和/或使用files参数。

您似乎尝试将application/x-www-form-urlencoded - 编码数据作为关键字参数发送;将它们放在data参数的字典中:

params = {
    'client_id': self.app_id,
    'client_secret': self.app_secret,
    'grant_type': "client_credentials"
}

response = requests.post(path, data=params)

请参阅快速入门文档的More complicated POST requests section

相关问题