Debian Codegraph @

docs/advanced/text-encodings.md

raw
1 When accessing `response.text`, we need to decode the response bytes into a unicode text representation.
2
3 By default `httpx` will use `"charset"` information included in the response `Content-Type` header to determine how the response bytes should be decoded into text.
4
5 In 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.
6
7 ## Using the default encoding
8
9 To understand this better let's start by looking at the default behaviour for text decoding...
10
11 ```python
12 import httpx
13 # Instantiate a client with the default configuration.
14 client = httpx.Client()
15 # Using the client...
16 response = client.get(...)
17 print(response.encoding) # This will either print the charset given in
18 # the Content-Type charset, or else "utf-8".
19 print(response.text) # The text will either be decoded with the Content-Type
20 # charset, or using "utf-8".
21 ```
22
23 This 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.
24
25 ## Using an explicit encoding
26
27 In 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.
28
29 ```python
30 import httpx
31 # Instantiate a client with a Japanese character set as the default encoding.
32 client = httpx.Client(default_encoding="shift-jis")
33 # Using the client...
34 response = client.get(...)
35 print(response.encoding) # This will either print the charset given in
36 # the Content-Type charset, or else "shift-jis".
37 print(response.text) # The text will either be decoded with the Content-Type
38 # charset, or using "shift-jis".
39 ```
40
41 ## Using auto-detection
42
43 In 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.
44
45 To 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.
46
47 There are two widely used Python packages which both handle this functionality:
48
49 * [`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.
51
52 Let's take a look at installing autodetection using one of these packages...
53
54 ```shell
55 $ pip install httpx
56 $ pip install chardet
57 ```
58
59 Once `chardet` is installed, we can configure a client to use character-set autodetection.
60
61 ```python
62 import httpx
63 import chardet
64
65 def autodetect(content):
66 return chardet.detect(content).get("encoding")
67
68 # Using a client with character-set autodetection enabled.
69 client = httpx.Client(default_encoding=autodetect)
70 response = client.get(...)
71 print(response.encoding) # This will either print the charset given in
72 # the Content-Type charset, or else the auto-detected
73 # character set.
74 print(response.text)
75 ```