Skip to main content
 首页 » 编程设计

python之用于http header 构造的python字典中的变量

2024年10月24日8lautakyan007

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)