Search code examples
encodingpython-asynciopython-unicodeunicode-stringaiohttp

UnicodeEncodeError while using aiohttp in python3.6


I use aiohttp to make request to my url, but I don`t know why this error occurs!!!!!

async def get_location_data(url):
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                data = await response.json() 
                return data 
    except Exception:
        return None

while I get the response and I want to change items of my list, this error happens:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)

I have searched so much about it, some people said that I should use response.text(encoding="utf-8) or response.json(encoding="utf-8)

How can I fix this error?


Solution

  • As other people have said, use await response.json(encoding="utf-8").