Batu Lab NotesPractical developer guides

Decode a response body using a declared charset

By Batu · English technical notes

Decode a response body using a declared charset

A response body is bytes; a Content-Type field can supply the character encoding needed to turn those bytes into text. The fixture here declares text/plain; charset=iso-8859-1 and contains b"caf\xe9". email.message.Message stores the field, and get_content_charset() extracts its charset parameter. Passing that returned name to bytes.decode() yields the text café.

The assertions keep the example concrete. One checks the extracted codec name, one checks the resulting Unicode string, and a final byte comparison confirms that encoding the text with the declared codec recreates the original fixture. The example uses established standard-library APIs and has no newer API version requirement. Its stdout is deterministic because it prints the decoded Unicode text directly.

A charset parameter is not a guarantee that incoming bytes are valid for that encoding. bytes.decode() can raise UnicodeDecodeError under its default strict error policy, and this example intentionally lets that error be visible rather than silently replacing data. It also assumes the field exists and declares a usable charset; real clients need a documented fallback policy for missing or unsupported declarations. This demonstration does not fetch a response, sniff encodings, or decide whether the media type is appropriate for text.

Python documents case-insensitive header lookup and parameter-oriented message methods in the email.message documentation, while the bytes.decode() documentation describes decoding bytes to text.

AI-assistance disclosure: AI assisted the drafting of this educational example.

from email.message import Message

headers = Message()
headers["Content-Type"] = "text/plain; charset=iso-8859-1"
body = b"caf\xe9"

charset = headers.get_content_charset()
assert charset == "iso-8859-1"

text = body.decode(charset)
assert text == "café"
assert text.encode(charset) == body

print(text)
café