requests-cache / aiohttp-client-cache

An async persistent cache for aiohttp requests
MIT License
122 stars 22 forks source link

Error when mixing URL with keywords and params #98

Closed cheginit closed 3 years ago

cheginit commented 3 years ago

The problem

When the URL contains keywords, session.get (or session.post) convert those keywords into dictionaries and adds to the params (or data) argument but doesn't remove them from the URL. This leads to duplicate keywords in the requests that causes the request to fails.

Expected behavior

I tested the same code with aiohttp.ClientSession and it works without any issue so the issue is with CachedSession.

Steps to reproduce the behavior

import asyncio

from aiohttp_client_cache import CachedSession

URL = "https://labs.waterdata.usgs.gov/api/nldi/linked-data/comid/position?coords=POINT(-69.3 45.2)"
PARAM = {'f': 'json'}

async def fetch(session: CachedSession) -> None:
    async with session.get(URL, params=PARAM) as resp:
        print(URL, PARAM)
        print(resp.status)
        data = await resp.json()
        print(data)

async def go() -> None:
    async with CachedSession() as session:
        await fetch(session)

loop = asyncio.new_event_loop()
loop.run_until_complete(go())
loop.close()

For some reason, PARAM is changed in the session.get call to {'f': 'json', 'coords': 'POINT(-69.3 45.2)'}.

Workarounds

Pass all the keywords as params.

Environment

cheginit commented 3 years ago

Also, this happened after updating to 0.5.0 and wasn't in the previous versions.

JWCook commented 3 years ago

Looks like I broke this in #85. This should be easy to fix. Thanks for reporting it!

cheginit commented 3 years ago

Sure! Great.

JWCook commented 3 years ago

This is now fixed in 0.5.1. Looks like your example works as expected now.

Let me know if you run into any more issues!

cheginit commented 3 years ago

Thanks for the quick fix! Will you update it on the conda-forge repo as well?

JWCook commented 3 years ago

Yes, that's managed by the conda-forge bot on the repo here: https://github.com/conda-forge/aiohttp-client-cache-feedstock

It picks up new releases from PyPI and triggers a new conda release, usually within an hour or so.

JWCook commented 3 years ago

Looks like the latest version is on conda-forge now.

cheginit commented 3 years ago

Great! Now, I can release my package with your latest fix.