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

How to pass Authorization TOKEN into Python requests header

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

I am trying to make a http request using Python3.8.5 + requests 2.25.0. The request contains Authorization Token in the header.

When use Postman to make the request, I can get response without issue. But, when I use following python code to make the request, it returns 401. Not sure what is worng in the code.

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

Following are the python code having issues.

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

The problem resloved by adding a / at the end of the request url. :-)