Using requests module, how to handle 'set-cookie' in request response?
cookies, python, python-requests
Solution
Ignore the cookie-jar, let `requests` handle cookies for you. Use a `Session` object instead, it'll persist cookies and send them back to the server:
with requests.Session() as s:
r = s.get(url1)
r = s.post(url2, data="username and password data payload")
Problem
I'm attempting to open a login page (GET), fetch the cookies provided by the webserver, then submit a username and password pair to log into the site (POST). Looking at this Stackoverflow question/answer, I would think that I would just do the following: ``` import requests from http.cookiejar import CookieJar url1 = 'login prompt page' url2 = 'login submission URL' jar = CookieJar() r = requests.get(url1, cookies=jar) r2 = requests.post(url2, cookies=jar, data="username and password data payload") ``` However, in `r` there is a `Set-Cookie` in the headers, but that isn't changing the `jar` object. In fact, nothing is being populated into `jar` as the linked question's response would indicate. I'm getting around this in my code by having a headers dict and after doing the GET or POST, using this to handle the `Set-Cookie` header: ``` headers['Cookie'] = r.headers['set-cookie'] ``` Then passing around the headers in the requests methods. Is this correct, or is there a better way to apply the `Set-Cookie`?