Search code examples
pythoncookiesadditionaiohttp

How to add cookie in session in aiohttp


I want to add cookie in session like in requests library

session = requests.session()
session.cookies.set(line["name"], line["value"], domain=line["domain"])

I tried using cookiejar but it didn't work


Solution

  • You can use the cookies parameter in ClientSession to send your own cookies to the server. By using ClientSession you have access to a cookie_jar attribute which lets you pass access cookies to be used across multiple requests.

    from aiohttp import ClientSession
    
    
    async def my_async_function():
        cookies = {"cookie1": "value1", "cookie2": "value2"}
        async with ClientSession(cookies=cookies) as session:
            response = await session.get(...)
    

    This is the link to the documentation of the latest version of aiohttp