2

New to python but I'm trying to use a variable within a dictionary that is used to construct a http header

This is what I have:

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)

I have tried a few different combinations with no luck

1 Answer 1

4

If I understand you correctly you just want to concatenating the strings using the + operator:

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.