Python 新手,但我正在尝试使用字典中的变量来构造 http header
这就是我所拥有的:
import requests
url = "https://sample.com"
auth = "sampleauthtoken"
headers = {
'authorization': "Bearer "<VARIABLE auth HERE>,
'cache-control': "no-cache"
}
response = requests.request("GET", url, headers=headers)
print(response.text)
我尝试了几种不同的组合,但没有成功
最佳答案
如果我理解正确的话,您只想使用 +
运算符连接字符串:
import requests
url = "https://sample.com"
auth = "sampleauthtoken"
headers = {
'authorization': "Bearer " + auth, # -> "Bearer sampleauthtoken"
'cache-control': "no-cache"
}
response = requests.request("GET", url, headers=headers)
print(response.text)
关于python - python字典中的变量用于http header 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270320/