Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requests CookieJar empty even thought the page have it

I'm on Python 3.5.1, using requests, the relevant part of the code is as follows:

req = requests.post(self.URL, data={"username": username, "password": password}) 
self.cookies = {"MOODLEID1_": req.cookies["MOODLEID1_"], "MoodleSession": req.cookies["MoodleSession"]}

self.URL has the correct page, and the POST is working as intended, I did some print to check that, and it passed.

My output:

Traceback (most recent call last):
  File "D:/.../main.py", line 14, in <module>
    m.login('first.last', 'pa$$w0rd!')
  File "D:\...\moodle2.py", line 14, in login
    self.cookies = {"MOODLEID1_": req.cookies["MOODLEID1_"], "MoodleSession": req.cookies["MoodleSession"]}
  File "D:\...\venv\lib\site-packages\requests\cookies.py", line 287, in __getitem__
    return self._find_no_duplicates(name)
  File "D:\...\venv\lib\site-packages\requests\cookies.py", line 345, in _find_no_duplicates
    raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='MOODLEID1_', domain=None, path=None"

I'm trying to debug during runtime to check what req.cookies has. But what I get is surprising, at least for me. If you put a breakpoint on self.cookies = {...} and run [(c.name, c.value, c.domain) for c in req.cookies] I get an empty list, like there isn't any cookie in there.

The site does send cookies, checking with a Chrome extension, I found 2, "MOODLEID1_" and "MoodleSession", so why I'm not getting them?

like image 671
JChris Avatar asked Apr 14 '16 19:04

JChris


1 Answers

The response doesn't appear to contain any cookies. Look for one or more Set-Cookie headers in req.headers.

Cookies stored in a browser are there because a response included a Set-Cookie header for each of those cookies. You'll have to find what response the server sets those cookies with; apparently it is not this response.

If you need to retain those cookies (once set) across requests, do use a requests.Session() object; this'll retain any cookies returned by responses and send them out again as appropriate with new requests.

like image 131
Martijn Pieters Avatar answered Sep 25 '22 05:09

Martijn Pieters