python - python字典中的变量用于http header 构建

标签 python dictionary python-requests

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/

相关文章:

python - 如何在简单的 wsgi 应用程序中响应 ajax?

python - 如何在 scipy 中创建一个巨大的稀疏矩阵

python - 遍历嵌套字典并获取 Python 中的路径?

Python请求base64图片

python - 在 python 中以编程方式访问 API 端点时出现错误请求

python - Kaggle 上出现 '!pip install torchxrayvision' 连接错误

python - 使用python显示unicode字符

python - Python 中 dicT 的简单问题

python - 为什么迭代 dict 这么慢?

python - 如何在Python的请求库中模拟 session 中的副作用?