@@ -51,7 +51,8 @@ The client session supports the context manager protocol for self closing.
5151 read_bufsize=2**16, \
5252 requote_redirect_url=False, \
5353 trust_env=False, \
54- trace_configs=None)
54+ trace_configs=None, \
55+ fallback_charset_resolver=_chardet_resolver)
5556
5657 The class for creating client sessions and making requests.
5758
@@ -200,6 +201,18 @@ The client session supports the context manager protocol for self closing.
200201 disabling. See :ref: `aiohttp-client-tracing-reference ` for
201202 more information.
202203
204+ :param Callable[[ClientResponse,bytes],str] fallback_charset_resolver:
205+ A :term: `callable ` that accepts a :class: `ClientResponse ` and the
206+ :class: `bytes ` contents, and returns a :class: `str ` which will be used as
207+ the encoding parameter to :meth: `bytes.decode() `.
208+
209+ This function will be called when the charset is not known (e.g. not specified in the
210+ Content-Type header). The default function in 3.8.6 calls ``chardetng ``
211+ or ``charset-normaliser ``. In 3.9+ this be replaced with a function that
212+ simply defaults to ``utf-8 ``.
213+
214+ .. versionadded :: 3.8.6
215+
203216 .. attribute :: closed
204217
205218 ``True `` if the session has been closed, ``False `` otherwise.
@@ -1400,12 +1413,8 @@ Response object
14001413 Read response's body and return decoded :class: `str ` using
14011414 specified *encoding * parameter.
14021415
1403- If *encoding * is ``None `` content encoding is autocalculated
1404- using ``Content-Type `` HTTP header and *charset-normalizer * tool if the
1405- header is not provided by server.
1406-
1407- :term: `cchardet ` is used with fallback to :term: `charset-normalizer ` if
1408- *cchardet * is not available.
1416+ If *encoding * is ``None `` content encoding is determined from the
1417+ Content-Type header, or using the ``fallback_charset_resolver `` function.
14091418
14101419 Close underlying connection if data reading gets an error,
14111420 release connection otherwise.
@@ -1414,10 +1423,7 @@ Response object
14141423 ``None `` for encoding autodetection
14151424 (default).
14161425
1417- :return str: decoded *BODY *
14181426
1419- :raise LookupError: if the encoding detected by cchardet is
1420- unknown by Python (e.g. VISCII).
14211427
14221428 .. note ::
14231429
@@ -1430,18 +1436,15 @@ Response object
14301436
14311437 await resp.text('ISO-8859-1')
14321438
1433- .. comethod :: json(*, encoding=None, loads=json.loads, \
1439+ .. method :: json(*, encoding=None, loads=json.loads, \
14341440 content_type='application/json')
1441+ :async:
14351442
14361443 Read response's body as *JSON *, return :class: `dict ` using
14371444 specified *encoding * and *loader *. If data is not still available
1438- a ``read `` call will be done,
1445+ a ``read `` call will be done.
14391446
1440- If *encoding * is ``None `` content encoding is autocalculated
1441- using :term: `cchardet ` or :term: `charset-normalizer ` as fallback if
1442- *cchardet * is not available.
1443-
1444- if response's `content-type ` does not match `content_type ` parameter
1447+ If response's `content-type ` does not match `content_type ` parameter
14451448 :exc: `aiohttp.ContentTypeError ` get raised.
14461449 To disable content type check pass ``None `` value.
14471450
@@ -1473,17 +1476,9 @@ Response object
14731476
14741477 .. method :: get_encoding()
14751478
1476- Automatically detect content encoding using ``charset `` info in
1477- ``Content-Type `` HTTP header. If this info is not exists or there
1478- are no appropriate codecs for encoding then :term: `cchardet ` /
1479- :term: `charset-normalizer ` is used.
1480-
1481- Beware that it is not always safe to use the result of this function to
1482- decode a response. Some encodings detected by cchardet are not known by
1483- Python (e.g. VISCII). *charset-normalizer * is not concerned by that issue.
1484-
1485- :raise RuntimeError: if called before the body has been read,
1486- for :term: `cchardet ` usage
1479+ Retrieve content encoding using ``charset `` info in ``Content-Type `` HTTP header.
1480+ If no charset is present or the charset is not understood by Python, the
1481+ ``fallback_charset_resolver `` function associated with the ``ClientSession `` is called.
14871482
14881483 .. versionadded :: 3.0
14891484
0 commit comments