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)
Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25
Flex Code
  • 21
  • 7
  • You can easily [interpolate strings](http://stackoverflow.com/questions/11788472/does-python-has-a-similar-variable-interpolation-like-string-var-in-ruby) or [concatenate them](https://www.google.com/search?q=string+concatenation+python) using the `+` operator. Is there anything else that needs to be done here? – Anderson Green Mar 24 '17 at 20:59

2 Answers2

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(:) .

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25
-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)
}
Barmar
  • 741,623
  • 53
  • 500
  • 612