title: Using requests module, how to handle 'set-cookie' in request response?
body: <p>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).</p>
  <p>Looking at <a href="https://stackoverflow.com/questions/6878418/putting-a-cookie-in-a-cookiejar">this Stackoverflow question/answer</a>, I would think that I would just do the following:</p>
  <pre><code>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=&quot;username and password data payload&quot;)
  </code></pre>
  <p>However, in <code>r</code> there is a <code>Set-Cookie</code> in the headers, but that isn't changing the <code>jar</code> object.  In fact, nothing is being populated into <code>jar</code> as the linked question's response would indicate.</p>
  <p>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 <code>Set-Cookie</code> header:</p>
  <pre><code>headers['Cookie'] = r.headers['set-cookie']
  </code></pre>
  <p>Then passing around the headers in the requests methods.  Is this correct, or is there a better way to apply the <code>Set-Cookie</code>?</p>