text-encodings.md
This file
Path
docs/advanced/text-encodings.md
75 lines · 3.6 KiB · Markdown
Last changed by
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
History of this fileTake it
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 1–75why, one step back1When accessing `response.text`, we need to decode the response bytes into a unicode text representation.23By default `httpx` will use `"charset"` information included in the response `Content-Type` header to determine how the response bytes should be decoded into text.45In cases where no charset information is included on the response, the default behaviour is to assume "utf-8" encoding, which is by far the most widely used text encoding on the internet.67## Using the default encoding89To understand this better let's start by looking at the default behaviour for text decoding...1011```python12import httpx13# Instantiate a client with the default configuration.14client = httpx.Client()15# Using the client...16response = client.get(...)17print(response.encoding) # This will either print the charset given in18 # the Content-Type charset, or else "utf-8".19print(response.text) # The text will either be decoded with the Content-Type20 # charset, or using "utf-8".21```2223This is normally absolutely fine. Most servers will respond with a properly formatted Content-Type header, including a charset encoding. And in most cases where no charset encoding is included, UTF-8 is very likely to be used, since it is so widely adopted.2425## Using an explicit encoding2627In some cases we might be making requests to a site where no character set information is being set explicitly by the server, but we know what the encoding is. In this case it's best to set the default encoding explicitly on the client.2829```python30import httpx31# Instantiate a client with a Japanese character set as the default encoding.32client = httpx.Client(default_encoding="shift-jis")33# Using the client...34response = client.get(...)35print(response.encoding) # This will either print the charset given in36 # the Content-Type charset, or else "shift-jis".37print(response.text) # The text will either be decoded with the Content-Type38 # charset, or using "shift-jis".39```4041## Using auto-detection4243In cases where the server is not reliably including character set information, and where we don't know what encoding is being used, we can enable auto-detection to make a best-guess attempt when decoding from bytes to text.4445To use auto-detection you need to set the `default_encoding` argument to a callable instead of a string. This callable should be a function which takes the input bytes as an argument and returns the character set to use for decoding those bytes to text.4647There are two widely used Python packages which both handle this functionality:4849* [`chardet`](https://chardet.readthedocs.io/) - This is a well established package, and is a port of [the auto-detection code in Mozilla](https://www-archive.mozilla.org/projects/intl/chardet.html).50* [`charset-normalizer`](https://charset-normalizer.readthedocs.io/) - A newer package, motivated by `chardet`, with a different approach.5152Let's take a look at installing autodetection using one of these packages...5354```shell55$ pip install httpx56$ pip install chardet57```5859Once `chardet` is installed, we can configure a client to use character-set autodetection.6061```python62import httpx63import chardet6465def autodetect(content):66 return chardet.detect(content).get("encoding")6768# Using a client with character-set autodetection enabled.69client = httpx.Client(default_encoding=autodetect)70response = client.get(...)71print(response.encoding) # This will either print the charset given in72 # the Content-Type charset, or else the auto-detected73 # character set.74print(response.text)75```