I have read all the questions I could find relating to this topic but none were helpful and/or relevant.

I am using playwright with Asyncio and aiohttp to automate some web based tasks. Part of that workflow involves getting and re-using cookies. I use playwright's context.storage_state() to get the everything from storage. It returns a dict with everything in storage like this:

{
  "cookies": [
    {
        'name': 'cookie_name', 
        'value': 'cookie_value', 
        'domain': '.website.com', 
        'path': '/', 
        'expires': 1234567890, 
        'httpOnly': False, 
        'secure': False, 
        'sameSite': 'None'
    },
    ...
  ],
  "origins": [
    {
      "origin": "https://www.website.com",
      "localStorage": [
        {
          "name": "name",
          "value": "value"
        },
        ...
      ]
    }
  ]
}

I grab the cookies using storage['cookies'] and then loop over them attempting to add each one to the aiohttp.CookieJar(), which I am pretty sure its just an http.cookiejar.CookieJar() object. Every time I try to load one of these dicts into the cookie jar as a cookie I get a

CookieError: Attempt to set a reserved key 'domain'

I have looked at the RFC2109 standard and the source code: https://github.com/python/cpython/blob/3.9/Lib/http/cookies.py

No matter what I try I get an error.

I have tried:

jar = aiohttp.CookieJar() # basically http.cookiejar.CookieJar I think
storage = context.storage_state()

for cookie in storage['cookies']:
    jar.update_cookies(cookie)
for item in storage['cookies']:
    cookie = BaseCookie().load(str(item)) # not even sure why I tried this
    jar.update_cookies(cookie)
for item in storage['cookies']:
    cookie = BaseCookie().load(item)
    jar.update_cookies(cookie)
for item in storage['cookies']:
    cookie = SimpleCookie().load(item)
    jar.update_cookies(cookie)

I also tried removing all the 'reserved' keys from the cookie dict, even though that is the majority of the cookie's data. No matter what it won't let me load actual cookies into the cookie jar. I read through the HTTP cookie.py source code and docs and there is nothing in there as far as I can tell explaining what is wrong with what I am doing.

Anyone ever been able to load a cookie into the cookie jar object without doing it through a session?

1

There are 1 answers

0
Sadie Xar On

You didn't set the cookie properly. You have

  # 1. Set the fields of the cookie.
  #    Give the cookie a value from the 'yourname' variable,
  #    a domain (localhost), and a max-age.
  c['value'] = yourname
  c['domain'] = 'localhost'
  c['max_age'] = 60

and the solution is

  c['yourname'] = yourname
  c['yourname']['domain'] = 'localhost'
  c['yourname']['max-age'] = 60