api encoding ai_generated true

Response body garbled — Content-Type charset mismatch

ID: api/content-type-charset-mismatch

Also available as: JSON · Markdown
88%Fix Rate
87%Confidence
4Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

API returns Content-Type without charset=utf-8, client assumes latin-1 and gets mojibake.

generic

Workarounds

  1. 92% success Explicitly decode response body as UTF-8 regardless of header
    response.content.decode('utf-8')

    Sources: https://docs.python-requests.org/en/latest/user/advanced/#encodings

  2. 90% success Set response.encoding before accessing .text
    r = requests.get(url); r.encoding = 'utf-8'; data = r.json()

    Sources: https://docs.python-requests.org/en/latest/api/#requests.Response.encoding

Dead Ends

Common approaches that don't work:

  1. Force decode as ASCII 90% fail

    ASCII cannot represent multi-byte characters

  2. Add charset to Accept header expecting server to echo it 75% fail

    Most APIs ignore charset in Accept header