How to add a cookie to the cookiejar in python requests library
Quick Answer
Option 1
import requests
s = requests.session()
s.cookies.set("COOKIE_NAME", "the cookie works", domain="example.com")
Option 2
import requests
s = requests.session()
# Note that domain keyword parameter is the only optional parameter here
cookie_obj = requests.cookies.create_cookie(domain="example.com",name="COOKIE_NAME",value="the cookie works")
s.cookies.set_cookie(cookie_obj)
Detailed Answer
I do not know if this technique was valid when the original question was asked, but ideally you would generate your own cookie object using requests.cookies.create_cookie(name,value,**kwargs)
and then add it to the cookie jar via requests.cookies.RequestsCookieJar.set_cookie(cookie,*args,**kwargs)
. See the source/documentation here.
Adding a custom cookie to requests session
>>> import requests
>>> s = requests.session()
>>> print(s.cookies)
<RequestsCookieJar[]>
>>> required_args = {
'name':'COOKIE_NAME',
'value':'the cookie works'
}
>>> optional_args = {
'version':0,
'port':None,
#NOTE: If domain is a blank string or not supplied this creates a
# "super cookie" that is supplied to all domains.
'domain':'example.com',
'path':'/',
'secure':False,
'expires':None,
'discard':True,
'comment':None,
'comment_url':None,
'rest':{'HttpOnly': None},
'rfc2109':False
}
>>> my_cookie = requests.cookies.create_cookie(**required_args,**optional_args)
# Counter-intuitively, set_cookie _adds_ the cookie to your session object,
# keeping existing cookies in place
>>> s.cookies.set_cookie(my_cookie)
>>> s.cookies
<RequestsCookieJar[Cookie(version=0, name='COOKIE_NAME', value='the cookie works', port=None, port_specified=False, domain='www.domain.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>
Bonus: Lets add a super cookie then delete it
>>> my_super_cookie = requests.cookies.create_cookie('super','cookie')
>>> s.cookies.set_cookie(my_super_cookie)
# Note we have both our previous cookie and our new cookie
>>> s.cookies
<RequestsCookieJar[Cookie(version=0, name='super', value='cookie', port=None, port_specified=False, domain='', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='COOKIE_NAME', value='the cookie works', port=None, port_specified=False, domain='www.domain.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>
# Deleting is simple, note that this deletes the cookie based on the name,
# if you have multiple cookies with the same name it will raise
# requests.cookies.CookieConflictError
>>> del s.cookies['super']
>>> s.cookies
<RequestsCookieJar[Cookie(version=0, name='COOKIE_NAME', value='the cookie works', port=None, port_specified=False, domain='www.domain.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>
I found out a way to do it by importing CookieJar, Cookie, and cookies. With help from @Lukasa, he showed me a better way. However, with his way I was not able to specify the "port_specified", "domain_specified", "domain_initial_dot" or "path_specified" attributes. The "set" method does it automatically with default values. I'm trying to scrape a website and their cookie has different values in those attributes. As I am new to all of this I'm not sure if that really matters yet.
my_cookie = {
"version":0,
"name":'COOKIE_NAME',
"value":'true',
"port":None,
# "port_specified":False,
"domain":'www.mydomain.com',
# "domain_specified":False,
# "domain_initial_dot":False,
"path":'/',
# "path_specified":True,
"secure":False,
"expires":None,
"discard":True,
"comment":None,
"comment_url":None,
"rest":{},
"rfc2109":False
}
s = requests.Session()
s.cookies.set(**my_cookie)
To use the built in functions and methods...
import requests
session = requests.session()
my_cookies = {'cookie_key': 'cookie_value',
'another_cookie_key': 'another_cookie_value'}
requests.utils.add_dict_to_cookiejar(session.cookies, my_cookies)
You can add as many cookies as you need. If you need special headers, use this method to add them.
my_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'}
session.headers.update(my_headers)
If you're looking to handle a plain cookies str:
def make_cookiejar_dict(cookies_str):
# alt: `return dict(cookie.strip().split("=", maxsplit=1) for cookie in cookies_str.split(";"))`
cookiejar_dict = {}
for cookie_string in cookies_str.split(";"):
# maxsplit=1 because cookie value may have "="
cookie_key, cookie_value = cookie_string.strip().split("=", maxsplit=1)
cookiejar_dict[cookie_key] = cookie_value
return cookiejar_dict
cookies_str = '''nopubuser_abo=1; groupenctype_abo="1="'''
cj = requests.utils.cookiejar_from_dict(make_cookiejar_dict(cookies_str))
sess = requests.Session()
sess.cookies = cj