6

I'm trying to make a get requests on python using the Requests module while incorporating an existing Cookie, and here's what my code looks like:

import requests

url="https://stackoverflow.com/"
headers = {"User-Agent", "Mozilla/5.0"}
cookie = {
    "domain": ".stackoverflow.com",
    "expirationDate": "1458316186",
    "hostOnly": "false",
    "httpOnly": "false",
    "name": "__qca",
    "path": "/",
    "secure": "false",
    "session": "false",
    "storeId": "0",
    "value": "P0-SOMEVALUE-SOMEVALUE",
    "id": 1
}

print requests.get(url, cookies=cookie).text


Traceback (most recent call last):
  File "test.py", line 19, in <module>
    print requests.get(url, cookies=cookie).text
  File "C:\Python27\lib\site-packages\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 422, in request
    prep = self.prepare_request(req)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 360, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python27\lib\site-packages\requests\models.py", line 296, in prepare
    self.prepare_cookies(cookies)
  File "C:\Python27\lib\site-packages\requests\models.py", line 491, in prepare_cookies
    cookie_header = get_cookie_header(self._cookies, self)
  File "C:\Python27\lib\site-packages\requests\cookies.py", line 134, in get_cookie_header
    jar.add_cookie_header(r)
  File "C:\Python27\lib\cookielib.py", line 1326, in add_cookie_header
    attrs = self._cookie_attrs(cookies)
  File "C:\Python27\lib\cookielib.py", line 1285, in _cookie_attrs
    self.non_word_re.search(cookie.value) and version > 0):
TypeError: expected string or buffer

Not entirely sure what I'm doing wrong...

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • I removed your new problem; that's an entirely separate issue, see [Python, Unicode, and the Windows console](http://stackoverflow.com/q/5419) – Martijn Pieters Sep 24 '14 at 15:15

1 Answers1

6

Cookies are supposed to be just key-value pairs. You included far more, you included all the metadata a browser tracks for cookies, governing how such cookies can be returned or accessed by client side code.

Make your cookie just the one key-value pair:

cookies = {'__qca': 'P0-SOMEVALUE-SOMEVALUE'}

Everything else in your mapping is not part of the Cookie header sent to the server.

In this specific case it is the 'id': 1 key-value pair that throws the exception, because requests expected the value of what it sees as the id cookie to be a string, not an integer.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks! Seems like it worked and managed to get the request correctly but there's an issue transcribing the response to the text html... any idea? – Stupid.Fat.Cat Sep 24 '14 at 15:12
  • 1
    @Stupid.Fat.Cat: that issue is one with your console, see [Python, Unicode, and the Windows console](http://stackoverflow.com/q/5419). Moreover, that's a new issue, separate from the cookies problem. – Martijn Pieters Sep 24 '14 at 15:13