Skip to content

Cookies not loaded in response.cookies but present in response.headers #11105

Closed
@chemelli74

Description

@chemelli74

Describe the bug

While working on a library swap from httpx to aiohttp, investigating why the latest was not working, I found out that cookies are not loaded.

Using the following code:

resp = await self.session.request(POST, url)         
        
_LOGGER.warning("Received cookies: %s", resp.cookies)
_LOGGER.warning("Received headers: %s", resp.headers)

I got the following outputs:

Cookies

2025-05-31 12:17:19.298 WARNING (MainThread) [aioamazondevices] Received cookies: Set-Cookie: UserPref=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: ap-fid=""; Domain=amazon.it; expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/ap/; Secure
Set-Cookie: at-acbit=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: lc-acbit=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: sess-at-acbit=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: session-id=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/; Secure
Set-Cookie: session-id-time=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/; Secure
Set-Cookie: session-token=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: ubid-acbit=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: x-acbit=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/
Set-Cookie: x-wl-uid=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/

Headers (truncated for reporting)

2025-05-31 12:17:19.299 WARNING (MainThread) [aioamazondevices] Received headers <CIMultiDictProxy(
'Content-Type': 'text/html;charset=UTF-8', 
'Transfer-Encoding': 'chunked', 
'Connection': 'keep-alive', 
'Server': 'Server', 
'Date': 'Sat, 31 May 2025 12:17:19 GMT', 
'Set-Cookie': 'ap-fid=""; Domain=.amazon.it; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/ap/; Secure', 
'Set-Cookie': 'session-id=260-2135165-7905862; Domain=.amazon.it; Expires=Sun, 31-May-2026 12:17:19 GMT; Path=/; Secure', 
'Set-Cookie': 'session-id-time=2379413839l; Domain=.amazon.it; Expires=Sun, 31-May-2026 12:17:19 GMT; Path=/; Secure', 
'Set-Cookie': 'session-id=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'session-id-time=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'session-token=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'ubid-acbit=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'at-acbit=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'lc-acbit=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'x-acbit=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'x-wl-uid=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'sess-at-acbit=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'UserPref=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'X-XSS-Protection': '1', 
[...]

Focusing on session-id, in the cookies (1st one) there is:

Set-Cookie: session-id=-; Domain=www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT; Path=/; Secure

while in the headers (2nd one):

'Set-Cookie': 'session-id=-; path=/; domain=.www.amazon.it; expires=Fri, 31-May-2013 12:17:19 GMT', 
'Set-Cookie': 'session-id=260-2135165-7905862; Domain=.amazon.it; Expires=Sun, 31-May-2026 12:17:19 GMT; Path=/; Secure',

To Reproduce

"""Test script."""

import asyncio
import base64
import secrets

import orjson
from aiohttp import ClientSession

# Amazon APP info
AMAZON_APP_BUNDLE_ID = "com.amazon.echo"
AMAZON_APP_ID = "MAPiOSLib/6.0/ToHideRetailLink"
AMAZON_APP_NAME = "AioAmazonDevices"
AMAZON_APP_VERSION = "2.2.556530.0"
AMAZON_DEVICE_SOFTWARE_VERSION = "35602678"
AMAZON_DEVICE_TYPE = "A2IVLV5VM2W81"
AMAZON_CLIENT_OS = "16.6"

DEFAULT_HEADERS = {
    "User-Agent": (
        f"Mozilla/5.0 (iPhone; CPU iPhone OS {AMAZON_CLIENT_OS.replace('.', '_')} like Mac OS X) "  # noqa: E501
        "AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
    ),
    "Accept-Language": "en-US",
    "Accept-Encoding": "gzip",
    "Connection": "keep-alive",
}
URL = "https://www.amazon.it/ap/signin"


async def main() -> None:
    """Start script."""
    session = ClientSession(
        headers=DEFAULT_HEADERS,
        cookies=await _build_init_cookies(),
    )
    resp = await session.request("POST", URL)

    print("Cookies:")
    print(resp.cookies)
    print("--------")
    print("Headers:")
    print(resp.headers)


async def _build_init_cookies() -> dict[str, str]:
    """Build initial cookies to prevent captcha in most cases."""
    token_bytes = secrets.token_bytes(313)
    frc = base64.b64encode(token_bytes).decode("ascii").rstrip("=")

    map_md_dict = {
        "device_user_dictionary": [],
        "device_registration_data": {
            "software_version": AMAZON_DEVICE_SOFTWARE_VERSION,
        },
        "app_identifier": {
            "app_version": AMAZON_APP_VERSION,
            "bundle_id": AMAZON_APP_BUNDLE_ID,
        },
    }
    map_md_str = orjson.dumps(map_md_dict).decode("utf-8")
    map_md = base64.b64encode(map_md_str.encode()).decode().rstrip("=")

    return {"amzn-app-id": AMAZON_APP_ID, "frc": frc, "map-md": map_md}


if __name__ == "__main__":
    asyncio.run(main())

Expected behavior

resp.cookies should have all the cookies set by the server

Logs/tracebacks

N/A

Python Version

Python 3.13.3

aiohttp Version

Name: aiohttp
Version: 3.12.6
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache-2.0
Location: /workspaces/aioamazondevices/.venv/lib/python3.13/site-packages
Requires: aiohappyeyeballs, aiosignal, attrs, frozenlist, multidict, propcache, yarl
Required-by: aioamazondevices

multidict Version

Name: multidict
Version: 6.4.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /workspaces/aioamazondevices/.venv/lib/python3.13/site-packages
Requires: 
Required-by: aiohttp, yarl

propcache Version

Name: propcache
Version: 0.3.1
Summary: Accelerated property cache
Home-page: https://github.com/aio-libs/propcache
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /workspaces/aioamazondevices/.venv/lib/python3.13/site-packages
Requires: 
Required-by: aiohttp, yarl

yarl Version

Name: yarl
Version: 1.20.0
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /workspaces/aioamazondevices/.venv/lib/python3.13/site-packages
Requires: idna, multidict, propcache
Required-by: aioamazondevices, aiohttp

OS

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpr-availablereproducer: presentThis PR or issue contains code, which reproduce the problem described or clearly understandable STR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions