I am using requests to fetch data from a resp API. The problem is that when I try to convert the response to UTF-8 it results in some broken characters, e.g.,
if I use response.text
I get
response.content = {"description":"Golden Cã£o Mb Adulto 3kg F"}
if I use response.content
I get
response.content = {"description":"Golden C\xc3\xa3\xc2\xa3o Mb Adulto 3kg F"}
I tried to change the request encode using response.encoding = 'utf-8'
, response.encoding = 'latin-1'
, and many others before the response.text
.
I tried response.decode('utf-8')
and others decoding as well. In this case I have {"description":"Golden Cã£o Mb Adulto 3kg F"}
I have many other things in this response, if I use
response.text.encode('latin-1').decode('utf-8')
I can fix some of these broken caracteres but for the above example I get this error
{UnicodeDecodeError}'utf-8' codec can't decode bytes ...: invalid continuation byte
I tried a lot of other things, but I could not fix this. I need some help.
Edit: Server's response headers
{
'Content-Type': 'application/json',
'Vary': 'Accept-Encoding',
'Content-Encoding': 'gzip',
'Content-Length': '1603',
'Connection': 'close'
}
For the exemple above the result should be
{"description":"Golden Cão Mb Adulto 3kg F"}
EDIT: Solved It turns out the error was in the server side. The server was corrupting some characters when saving them.