0

I've extracted some cookies in my python code in the form of a list of Dictionaries and each dictionary is a cookie like example below:

[
    {
        "domain": "x.com",
        "httpOnly": True,
        "name": "Username_COOKIE",
        "path": "/",
        "sameSite": "Lax",
        "secure": False,
        "value": "some value",
    },
    {
        "domain": "x.com",
        "expiry": 1676724072,
        "httpOnly": False,
        "name": "ASP.NET_SessionId",
        "path": "/",
        "sameSite": "Lax",
        "secure": False,
        "value": "some value",
    },
    {
        "domain": "x.com",
        "expiry": 1676810425,
        "httpOnly": False,
        "name": "_gid",
        "path": "/",
        "sameSite": "Lax",
        "secure": False,
        "value": "some value",
    }

Now I want to send a new GET request using these cookies. The request.get() method gets cookies in it's parameter as "RequestsCookieJar" object. My question is how can I make a "RequestsCookieJar" object using my dictionaries to pass it to the request.get() method?

RezA
  • 103
  • 1
  • 8
  • Does this answer your question? [Trying to load cookie into requests session from dictionary](https://stackoverflow.com/questions/31928942/trying-to-load-cookie-into-requests-session-from-dictionary) – dskrypa Feb 18 '23 at 21:21
  • @dskrypa No my cookies are in the form of a list of dictionaries and I want to make a "RequestsCookieJar" object. – RezA Feb 24 '23 at 08:27

1 Answers1

0

If you first alter the structure a bit with

cookieList = [  {'domain': 'x.com', 'httpOnly': True, 'name': 'Username_COOKIE', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
                {'domain': 'x.com', 'expiry': 1676724072, 'httpOnly': False, 'name': 'ASP.NET_SessionId', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
                {'domain': 'x.com', 'expiry': 1676810425, 'httpOnly': False, 'name': '_gid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'}  ]

for ci, c in enumerate(cookieList):
    if 'httpOnly' in c: 
        cookieList[ci]['rest'] = {'HttpOnly': c['httpOnly']}
        del cookieList[ci]['httpOnly']
    if 'expiry' in c:
        cookieList[ci]['expires'] = c['expiry']
        del cookieList[ci]['expiry']

so that cookieList looks like

[{'domain': 'x.com',
  'name': 'Username_COOKIE',
  'path': '/',
  'sameSite': 'Lax',
  'secure': False,
  'value': 'some value',
  'rest': {'HttpOnly': True}},
 {'domain': 'x.com',
  'name': 'ASP.NET_SessionId',
  'path': '/',
  'sameSite': 'Lax',
  'secure': False,
  'value': 'some value',
  'rest': {'HttpOnly': False},
  'expires': 1676724072},
 {'domain': 'x.com',
  'name': '_gid',
  'path': '/',
  'sameSite': 'Lax',
  'secure': False,
  'value': 'some value',
  'rest': {'HttpOnly': False},
  'expires': 1676810425}]

then you can turn it into a RequestsCookieJar object with

# import requests
cKeys = ['version', 'name', 'value', 'port', 'domain', 'path', 'secure', 
         'expires', 'discard', 'comment', 'comment_url', 'rest', 'rfc2109']

jar = requests.cookies.RequestsCookieJar()
for c in cookieList: jar.set(**{k:v for k,v in c.items() if k in cKeys})

You can now use jar like requests.get(url, cookies=jar), or view it with print(repr(jar)) which should output something like

<RequestsCookieJar[
  Cookie(version=0, name='ASP.NET_SessionId', value='some value', port=None, port_specified=False, domain='x.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=1676724072, discard=True, comment=None, comment_url=None, rest={'HttpOnly': False}, rfc2109=False), 
  Cookie(version=0, name='Username_COOKIE', value='some value', port=None, port_specified=False, domain='x.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': True}, rfc2109=False), 
  Cookie(version=0, name='_gid', value='some value', port=None, port_specified=False, domain='x.com', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=1676810425, discard=True, comment=None, comment_url=None, rest={'HttpOnly': False}, rfc2109=False)
]>

You might also be able to just get away with

cookieList = [  {'domain': 'x.com', 'httpOnly': True, 'name': 'Username_COOKIE', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
                {'domain': 'x.com', 'expiry': 1676724072, 'httpOnly': False, 'name': 'ASP.NET_SessionId', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'},
                {'domain': 'x.com', 'expiry': 1676810425, 'httpOnly': False, 'name': '_gid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'some value'}  ]

cookiesDict = {c['name']:c['value'] for c in cookieList if 'name' in c and 'value' in c}
resp = requests.get(url, cookies=cookiesDict)
Driftr95
  • 4,572
  • 2
  • 9
  • 21