Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_encoding_from_headers fails if charset name not specified #6646 #6659

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/requests/utils.py
Expand Up @@ -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"
Expand All @@ -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."""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Expand Up @@ -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):
Expand Down