0
  File "C:\Python33\lib\site-packages\requests\models.py", line 441, in prepare_headers
    for header in headers.items():
AttributeError: 'set' object has no attribute 'items'

How can i add a variable to my header? I'm trying to send a web request with cookies but i don't know how to add variables to the header part code:

headersx = {
    """
    'cookie': '__cfduid='%s'; PHPSESSID='%s'; lang=de; CF-RAY='%s',
    """
    %(cfuid, phpid, cfray)
}

response = requests.get('https://10minutemail.net/', headers=headersx)
1

2 Answers 2

0

You can achieve this something like this :

headersx = {
    'cookie': '__cfduid={}; PHPSESSID={}; lang=de; CF-RAY={}'.format(cfuid, phpid, cfray)
}

Alternatively, using String Formatting, you can do something like this :

headersx = {
    'cookie': '__cfduid=%s; PHPSESSID=%s; lang=de; CF-RAY=%s' % (cfuid, phpid, cfray)
}

For cfuid = 'a', phpid = 'b' and cfray = 'c', the headersx dictionary will result in :

{'cookie': '__cfduid=a; PHPSESSID=b; lang=de; CF-RAY=c'}

Note that dictionary needs to have key value pairs separated by colon(:) .

Sign up to request clarification or add additional context in comments.

Comments

-1

When creating a dictionary, you shouldn't put the whole key : value into a single string.

headersx = {
    'cookie': '__cfduid='%s'; PHPSESSID='%s'; lang=de; CF-RAY='%s'%(cfuid, phpid, cfray)
}

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.