Why Aiohttp Client Session Cookies May Not Persist Between Requests

Feb 22, 2024 ยท 2 min read

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 session, the cookies persist between requests.

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.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: