@@ -51,7 +51,8 @@ The client session supports the context manager protocol for self closing.
51
51
read_bufsize=2**16, \
52
52
requote_redirect_url=False, \
53
53
trust_env=False, \
54
- trace_configs=None)
54
+ trace_configs=None, \
55
+ fallback_charset_resolver=_chardet_resolver)
55
56
56
57
The class for creating client sessions and making requests.
57
58
@@ -200,6 +201,18 @@ The client session supports the context manager protocol for self closing.
200
201
disabling. See :ref: `aiohttp-client-tracing-reference ` for
201
202
more information.
202
203
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
+
203
216
.. attribute :: closed
204
217
205
218
``True `` if the session has been closed, ``False `` otherwise.
@@ -1400,12 +1413,8 @@ Response object
1400
1413
Read response's body and return decoded :class: `str ` using
1401
1414
specified *encoding * parameter.
1402
1415
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.
1409
1418
1410
1419
Close underlying connection if data reading gets an error,
1411
1420
release connection otherwise.
@@ -1414,10 +1423,7 @@ Response object
1414
1423
``None `` for encoding autodetection
1415
1424
(default).
1416
1425
1417
- :return str: decoded *BODY *
1418
1426
1419
- :raise LookupError: if the encoding detected by cchardet is
1420
- unknown by Python (e.g. VISCII).
1421
1427
1422
1428
.. note ::
1423
1429
@@ -1430,18 +1436,15 @@ Response object
1430
1436
1431
1437
await resp.text('ISO-8859-1')
1432
1438
1433
- .. comethod :: json(*, encoding=None, loads=json.loads, \
1439
+ .. method :: json(*, encoding=None, loads=json.loads, \
1434
1440
content_type='application/json')
1441
+ :async:
1435
1442
1436
1443
Read response's body as *JSON *, return :class: `dict ` using
1437
1444
specified *encoding * and *loader *. If data is not still available
1438
- a ``read `` call will be done,
1445
+ a ``read `` call will be done.
1439
1446
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
1445
1448
:exc: `aiohttp.ContentTypeError ` get raised.
1446
1449
To disable content type check pass ``None `` value.
1447
1450
@@ -1473,17 +1476,9 @@ Response object
1473
1476
1474
1477
.. method :: get_encoding()
1475
1478
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.
1487
1482
1488
1483
.. versionadded :: 3.0
1489
1484
0 commit comments