osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to load cookies from a json input in python-requests?


On 2019-08-12 11:38 p.m., Peng Yu wrote:
> ```
> import requests
> s = requests.Session()
> import json
> s.cookies.set_cookie(requests.utils.cookiejar_from_dict(json.load(sys.stdin)))
> ```
>
> I used the above command to load cookies from a json file. But I got
> the following error. Does anybody know how to fix the error? Thanks.
>
> ```
> Traceback (most recent call last):
>    File "/xxx/xxx.py", line 15, in <module>
>      s.cookies.set_cookie(requests.utils.cookiejar_from_dict(json.load(sys.stdin)))
>    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/cookies.py",
> line 345, in set_cookie
>      if hasattr(cookie.value, 'startswith') and
> cookie.value.startswith('"') and cookie.value.endswith('"'):
> AttributeError: 'RequestsCookieJar' object has no attribute 'value'
> ```
set_cookie is used to add an individual cookie in an existing cookiejar. 
You might want to have a look at 
requests.utils.add_dict_to_cookiejar(s.cookies, json.load(...)) or 
passing the result of requests.utils.cookiejar_from_dict ti the cookies 
argument of requests.Session