[Python] Loading cookies in JSON format into requests

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    I've got some cookies sent over from a client to allow me to log into his server programatically without needing the credentials.

    I'm using the requests package to make HTTP requests to his server. The problem is the cookies he has sent are all in JSON format and I can't seem to find any way to load these cookies.

    Aside from writing a method to convert them into regular cookies are there any existing solutions to load these cookies?

    Here is my current code:

    def cookieLogin(cookies):

    with open(cookies) as f:
    cookies = requests.utils.cookiejar_from_dict(json.load(f)[0] )

    session = requests.session()
    session.cookies = cookies

    r = session.get('https://example.com')


    Cookie example:

    [
    {
    "domain": ".example.com",
    "hostOnly": false,
    "httpOnly": false,
    "name": "act",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": false,
    "session": true,
    "storeId": "0",
    "value": "875",
    "id": 1
    },
    {
    "domain": ".example.com",
    "expirationDate": 1479135421.720188,
    "hostOnly": false,
    "httpOnly": false,
    "name": "c_user",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": true,
    "session": false,
    "storeId": "0",
    "value": "109",
    "id": 2
    },
    ...


    Error:

    Traceback (most recent call last):
    File "./cookieInviter.py", line 37, in <module>
    session = cookieLogin(args.cookies)
    File "/Users/jj/Projects/Jim/login.py", line 78, in cookieLogin
    r = session.get('https://example.com')

    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 487, in get
    return self.request('GET', url, **kwargs)
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 461, in request
    prep = self.prepare_request(req)
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 394, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
    File "/Library/Python/2.7/site-packages/requests/models.py", line 297, in prepare
    self.prepare_cookies(cookies)
    File "/Library/Python/2.7/site-packages/requests/models.py", line 518, in prepare_cookies
    cookie_header = get_cookie_header(self._cookies, self)
    File "/Library/Python/2.7/site-packages/requests/cookies.py", line 136, in get_cookie_header
    jar.add_cookie_header(r)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cookielib.py", line 1342, in add_cookie
    _header
    attrs = self._cookie_attrs(cookies)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cookielib.py", line 1301, in _cookie_at
    trs
    self.non_word_re.search(cookie.value) and version > 0):
    TypeError: expected string or buffer


    CookieJar:

    <RequestsCookieJar[<Cookie domain=.example.com for />, <Cookie hostOnly=False for />, <Cookie httpOnly=False for />,
    <Cookie id=1 for />, <Cookie name=act for />, <Cookie path=/ for />, <Cookie sameSite=no_restriction for />, <Cookie s
    ecure=False for />, <Cookie session=True for />, <Cookie storeId=0 for />, <Cookie value=... for />]>

    Continue reading...

Compartilhe esta Página