@@ -51,7 +51,8 @@ The client session supports the context manager protocol for self closing.
5151 read_bufsize=2**16, \
5252 requote_redirect_url=True, \
5353 trust_env=False, \
54- trace_configs=None)
54+ trace_configs=None, \
55+ fallback_charset_resolver=lambda r, b: "utf-8")
5556
5657 The class for creating client sessions and making requests.
5758
@@ -208,6 +209,16 @@ The client session supports the context manager protocol for self closing.
208209 disabling. See :ref: `aiohttp-client-tracing-reference ` for
209210 more information.
210211
212+ :param Callable[[ClientResponse,bytes],str] fallback_charset_resolver:
213+ A :term: `callable ` that accepts a :class: `ClientResponse ` and the
214+ :class: `bytes ` contents, and returns a :class: `str ` which will be used as
215+ the encoding parameter to :meth: `bytes.decode() `.
216+
217+ This function will be called when the charset is not known (e.g. not specified in the
218+ Content-Type header). The default function simply defaults to ``utf-8 ``.
219+
220+ .. versionadded :: 3.8.6
221+
211222 .. attribute :: closed
212223
213224 ``True `` if the session has been closed, ``False `` otherwise.
@@ -1406,12 +1417,8 @@ Response object
14061417 Read response's body and return decoded :class: `str ` using
14071418 specified *encoding * parameter.
14081419
1409- If *encoding * is ``None `` content encoding is autocalculated
1410- using ``Content-Type `` HTTP header and *charset-normalizer * tool if the
1411- header is not provided by server.
1412-
1413- :term: `cchardet ` is used with fallback to :term: `charset-normalizer ` if
1414- *cchardet * is not available.
1420+ If *encoding * is ``None `` content encoding is determined from the
1421+ Content-Type header, or using the ``fallback_charset_resolver `` function.
14151422
14161423 Close underlying connection if data reading gets an error,
14171424 release connection otherwise.
@@ -1420,35 +1427,21 @@ Response object
14201427 ``None `` for encoding autodetection
14211428 (default).
14221429
1423- :return str: decoded *BODY *
1424-
1425- :raise LookupError: if the encoding detected by cchardet is
1426- unknown by Python (e.g. VISCII).
14271430
1428- .. note ::
1431+ :raises: :exc: `UnicodeDecodeError ` if decoding fails. See also
1432+ :meth: `get_encoding `.
14291433
1430- If response has no ``charset `` info in ``Content-Type `` HTTP
1431- header :term: `cchardet ` / :term: `charset-normalizer ` is used for
1432- content encoding autodetection.
1433-
1434- It may hurt performance. If page encoding is known passing
1435- explicit *encoding * parameter might help::
1436-
1437- await resp.text('ISO-8859-1')
1434+ :return str: decoded *BODY *
14381435
14391436 .. method :: json(*, encoding=None, loads=json.loads, \
14401437 content_type='application/json')
14411438 :async:
14421439
14431440 Read response's body as *JSON *, return :class: `dict ` using
14441441 specified *encoding * and *loader *. If data is not still available
1445- a ``read `` call will be done,
1442+ a ``read `` call will be done.
14461443
1447- If *encoding * is ``None `` content encoding is autocalculated
1448- using :term: `cchardet ` or :term: `charset-normalizer ` as fallback if
1449- *cchardet * is not available.
1450-
1451- if response's `content-type ` does not match `content_type ` parameter
1444+ If response's `content-type ` does not match `content_type ` parameter
14521445 :exc: `aiohttp.ContentTypeError ` get raised.
14531446 To disable content type check pass ``None `` value.
14541447
@@ -1480,17 +1473,9 @@ Response object
14801473
14811474 .. method :: get_encoding()
14821475
1483- Automatically detect content encoding using ``charset `` info in
1484- ``Content-Type `` HTTP header. If this info is not exists or there
1485- are no appropriate codecs for encoding then :term: `cchardet ` /
1486- :term: `charset-normalizer ` is used.
1487-
1488- Beware that it is not always safe to use the result of this function to
1489- decode a response. Some encodings detected by cchardet are not known by
1490- Python (e.g. VISCII). *charset-normalizer * is not concerned by that issue.
1491-
1492- :raise RuntimeError: if called before the body has been read,
1493- for :term: `cchardet ` usage
1476+ Retrieve content encoding using ``charset `` info in ``Content-Type `` HTTP header.
1477+ If no charset is present or the charset is not understood by Python, the
1478+ ``fallback_charset_resolver `` function associated with the ``ClientSession `` is called.
14941479
14951480 .. versionadded :: 3.0
14961481
0 commit comments