2014-09-25 274 views
0

它确实应该是或者是否存在某些问题?Python请求在POST请求中发送Content-Length 0

我只是做它,因为它是在doc这样提到:

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'} 

什么错误我在做什么?

回答

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) 

参考Quickstart文件的More complicated POST requests section