Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeDecodeError: 'utf-8' codec can't decode byte ... #3270

Closed
Thempra opened this issue Sep 16, 2018 · 4 comments
Closed

UnicodeDecodeError: 'utf-8' codec can't decode byte ... #3270

Thempra opened this issue Sep 16, 2018 · 4 comments

Comments

@Thempra
Copy link

Thempra commented Sep 16, 2018

Long story short

When request a call to some servers, a coding error occurs instead download the image / content

Actual behaviour

Traceback (most recent call last):
  File "aiohttpPoC.py", line 16, in <module>
    loop.run_until_complete(main())
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 568, in run_until_complete
    return future.result()
  File "aiohttpPoC.py", line 13, in main
    html = await fetch(session, imageurl)
  File "aiohttpPoC.py", line 8, in fetch
    return await response.text()
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 921, in text
    return self._body.decode(encoding, errors=errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 6: invalid continuation byte

Steps to reproduce

import aiohttp
import asyncio
import datetime
import pytz

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    imageurl='https://www.aemet.es/imagenes_d/eltiempo/observacion/radar/'+datetime.datetime.now(pytz.timezone('Atlantic/Cape_Verde')).strftime("%Y%m%d%H%M")[:-1]+'0_r8va.gif'
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, imageurl)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Your environment

Python 3.7.0
aiohttp 3.4.4

@asvetlov
Copy link
Member

GitMate.io thinks possibly related issues are #1731 (UnicodeEncodeError: 'utf-8' codec can't encode character '\udca9'), #207 (UnicodeEncodeError: 'utf-8' codec can't encode character '\udcc3' in position 60: surrogates not allowed), #1750 (Encoding is always UTF-8 in POST data), #18 (Auto-decoding doesn't recognize content-type: application/json; charset=utf-8), and #1802 (response.text() crash if body contain non UTF-8 symbol).

@asvetlov asvetlov added the bug label Sep 16, 2018
@asvetlov
Copy link
Member

You are trying to fetch GIF as UTF-8 encoded text.
Obviously, this is an error.
What behavior do you expect?

@Thempra
Copy link
Author

Thempra commented Sep 16, 2018

rewrite again and now it's ok, thx !!

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.content.read()

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants