aio-libs / aio-libs/yarl

Comma not being encoded in GET request params

Offen
#210 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
1.5k
Forks
215
Ø Merge
1 T. 2 Min.
Gemergte PRs (30 T.)
13

Beschreibung

I am trying use `aiohttp` to send a GET request to the following URL: `https://api-fxtrade.oanda.com/v1/prices?instruments=EUR_USD%2CUSD_JPY`. However, I keep getting errors saying my request has a malformed query string:

```python
url = "https://api-fxtrade.oanda.com/v1/prices"
params = {'instruments': 'EUR_USD,USD_JPY'}

async with ClientSession() as s:
async with s.get(url, params=params, headers=headers) as r:
print(r.status, r.url)
# 400 https://api-fxtrade.oanda.com/v1/prices?instruments=EUR_USD,USD_JPY
```
Replacing `params` with `{'instruments': 'EUR_USD%2CUSD_JPY'}` doesn't work either. (It seems the URL-encoded string is decoded and never encoded again).

However, the equivalent code in `requests` works just fine:

```python
r = requests.get(url, headers=headers, params={'instruments': 'EUR_USD,USD_JPY'})
print(r.status_code, r.url)
# 200 https://api-fxtrade.oanda.com/v1/prices?instruments=EUR_USD%2CUSD_JPY
```

The workaround I found was to not use `params` at all (as mentioned in [aiohttp docs](https://aiohttp.readthedocs.io/en/stable/client_quickstart.html#passing-parameters-in-urls)):
```python
from urllib.parse import urlencode
from yarl import URL

url = URL('?'.join([url, urlencode(params)]), encoded=True)

async with ClientSession() as s:
async with s.get(url, headers=headers) as r:
print(r.status, r.url)
# 200 https://api-fxtrade.oanda.com/v1/prices?instruments=EUR_USD%2CUSD_JPY
```

Should this be thought as a bug or is this expected behavior?

Related: https://github.com/requests/requests/issues/794#issuecomment-7852135

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.