Skip to content

Fix: Handle both uppercase and lowercase Content-Type header keys#7241

Closed
yyyhakuna wants to merge 1 commit intopsf:mainfrom
yyyhakuna:fix/charset-detection-7234
Closed

Fix: Handle both uppercase and lowercase Content-Type header keys#7241
yyyhakuna wants to merge 1 commit intopsf:mainfrom
yyyhakuna:fix/charset-detection-7234

Conversation

@yyyhakuna
Copy link
Copy Markdown

Summary

Fix for issue #7234: Quoted charset values are not detected by get_encoding_from_headers

Problem

The get_encoding_from_headers function was only checking for lowercase content-type key, but HTTP headers are often passed with uppercase Content-Type. This caused the function to return None when headers had uppercase keys, even when a valid charset was present.

Solution

Check for both content-type (lowercase) and Content-Type (uppercase) header keys.

Test

from requests.utils import get_encoding_from_headers

headers = {"Content-Type": 'text/html; charset="utf-8"'}
print(get_encoding_from_headers(headers))  # Now returns 'utf-8' instead of None

Fixes

#7234

The get_encoding_from_headers function was only checking for lowercase
'content-type' key, but HTTP headers are often passed with uppercase
'Content-Type'. This caused the function to return None when headers
had uppercase keys, even when a valid charset was present.

Fixes: psf#7234
@nateprewitt nateprewitt closed this Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants