Warm tip: This article is reproduced from serverfault.com, please click

其他-如何将Authorization TOKEN传递给Python请求标头

(其他 - How to pass Authorization TOKEN into Python requests header)

发布于 2020-12-11 21:59:41

我正在尝试使用Python3.8.5 +请求2.25.0发出一个http请求。该请求的标头中包含授权令牌。

使用 Postman 发出请求时,我可以毫无问题地得到答复。但是,当我使用以下python代码发出请求时,它返回401。不确定代码中的内容是什么。

401  {"detail":"Authentication credentials were not provided."}

以下是有问题的python代码。

import requests

url = <MY_URL>

payload={}
headers = {
  'Authorization': 'TOKEN <MY_TOKEN>'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)
Questioner
Xiuyu Yun
Viewed
0
Xiuyu Yun 2020-12-29 07:11:02

通过在请求URL的末尾添加/来解决该问题。:-)