简体   繁体   中英

Passing unicode via aiohttp url

I am trying to make asynchronous GET function, but when url contains unicode(i used Korean letter) string, it occurs 400 Bad Request . There is no problem when i use requests, but only aiohttp gives me an error. How can i handle this problem?

requests

import requests

def get():
    response = requests.get("some url with unicode")
    return response

get()    # 200 OK

aiohttp

import asyncio
import aiohttp

async def get():
    response = await aiohttp.request("GET", "some url with unicode")
    return response

loop = asyncio.get_event_loop()
loop.run_until_complete(get())    # 400 Bad Request

I am using Python 3.6 , asyncio 3.4.3 , aiohttp 1.0.5 .

I found solution myself.

from urllib import parse

url = "some url with unicode"
parsed = parse.urlparse(url)
parsed = parse.parse_qs(parsed.query)
encoded = parse.urlencode(parsed, doseq=True)
url = "{}?{}".format("url base without parameters", encoded)

Then url is encoded ascii string, and i got 200 OK . Thanks for comments :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM