When making multiple requests with the aiohttp client, you may notice session cookies do not persist between requests like you might expect. This can be frustrating if you are trying to maintain a logged in state or session ID.
The reason this happens is that the aiohttp client uses a new connector and session for every client instance. Each request gets its own set of cookies that are isolated from other requests. Here's a simple example:
import aiohttp
async with aiohttp.ClientSession() as session:
await session.get("http://example.com/login")
# cookies from login are now gone
async with aiohttp.ClientSession() as session:
await session.get("http://example.com/user_page")
The second request will not have the login session cookie, so likely fail if a logged in user is required.
Preserving Cookies Between Requests
The key is to reuse the same client session for all requests that should share cookies:
import aiohttp
session = aiohttp.ClientSession()
await session.get("http://example.com/login")
# session is preserved
await session.get("http://example.com/user_page")
await session.close()
By reusing the
Another option is to manually specify cookies on a per-request basis if you know the names and values. But reusing the session is simpler if you want to maintain the state.
Overall, being aware that aiohttp client sessions are isolated by default can prevent unexpected issues with server-set cookies not persisting. Reusing the session keeps everything stateful.