Putting a `Cookie` in a `CookieJar`

Ram Rachum picture Ram Rachum · Jul 29, 2011 · Viewed 59.7k times · Source

I'm using the new Python Requests library to make http requests. I obtain a cookie from the server as text. How do I turn that into a CookieJar with the cookie in it?

Answer

dstanek picture dstanek · Aug 3, 2011

I'm confused by this question. The requests library will put the cookies in the jar for you.

import requests
import cookielib


URL = '...whatever...'
jar = cookielib.CookieJar()
r = requests.get(URL, cookies=jar)
r = requests.get(URL, cookies=jar)

The first request to the URL will fill the jar. The second request will send the cookies back to the server. The same goes for the standard library's urllib module cookielib. (doc currently available for 2.x Version)