You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been using this code in requests to save cookies to a file.
session = requests.Session()
json.dump(session.cookies.get_dict(), open(cookies.txt, 'w'))
and this code to open cookies file and set it to session cookies
cookies = requests.utils.cookiejar_from_dict(json.load(open('Cookies/cookies.txt', 'r')))
session.cookies.update(cookies)
How i do that in httpx? because httpx.Client.cookies doesn't have get_dict attribute.
Cookies instances are mappings (the docs say "a dict-like cookie store", and "standard mutable mapping interface"), so you can pass them to dict() to get a JSON-serializable dict object back. Similarly Client(cookies=…) accepts a dict, so you can pass the result of json.load() to it.
Activity
florimondmanca commentedon Jul 10, 2020
Hi,
Cookiesinstances are mappings (the docs say "a dict-like cookie store", and "standard mutable mapping interface"), so you can pass them todict()to get a JSON-serializable dict object back. SimilarlyClient(cookies=…)accepts adict, so you can pass the result ofjson.load()to it.Example:
Hope this helps!
Note that there seems to be some caveats to this "dump-and-load cookies" approach with some websites, see #895.