这是我的代码:
access_token = get_access_token()
bearer_token = base64.b64encode(bytes("'Bearer {}'".format(access_token)), 'utf-8')
headers = {'Content-Type': 'application/json', 'Authorization': bearer_token}
data = '{"FirstName" : "Jane", "LastName" : "Smith"}'
response = requests.post('https://myserver.com/endpoint', headers=headers, data=data)
如您所见,我在请求参数中手动设置了Authorization
标头,但实际请求的标头缺失:
{'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.4.3 CPython/2.7.9 Linux/4.1.19-v7+'}
。
另外需要提供的信息是,如果我将POST请求更改为GET请求,则Authorization
标头可以正常传递!
为什么这个库会在POST请求中删除标头,如何让它正常工作?
使用 v2.4.3 的 requests 库和 Python 2.7.9。
~/.netrc
文件,并将其重命名。 这可能不是OP的问题,但具有类似的症状。 - MarkHu