diff --git a/src/requests/utils.py b/src/requests/utils.py index a603a8638c..44fc70d251 100644 --- a/src/requests/utils.py +++ b/src/requests/utils.py @@ -549,7 +549,15 @@ def get_encoding_from_headers(headers): content_type, params = _parse_content_type_header(content_type) if "charset" in params: - return params["charset"].strip("'\"") + charset = params["charset"] + # Check if charset is a boolean value + if charset is True: + return "ISO-8859-1" + # Check if charset is explicitly False + elif charset is False: + return None + else: + return charset.strip("'\"") if "text" in content_type: return "ISO-8859-1" @@ -558,6 +566,8 @@ def get_encoding_from_headers(headers): # Assume UTF-8 based on RFC 4627: https://www.ietf.org/rfc/rfc4627.txt since the charset was unset return "utf-8" + return None + def stream_decode_response_unicode(iterator, r): """Stream decodes an iterator.""" diff --git a/tests/test_utils.py b/tests/test_utils.py index 8988eaf69c..4043a1928f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -617,6 +617,11 @@ def test__parse_content_type_header(value, expected): "utf-8", ), (CaseInsensitiveDict({"content-type": "text/plain"}), "ISO-8859-1"), + (CaseInsensitiveDict({"content-type": "text/html; charset"}), "ISO-8859-1"), + ( + CaseInsensitiveDict({"content-type": "application/json; charset"}), + "ISO-8859-1", + ), ), ) def test_get_encoding_from_headers(value, expected):