get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)

Open Beginner friendly
#7,644 1 comment 0 reactions 0 assignees View on GitHub

@suyashgitt is already working on this.

Since Nov 13, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Read src/requests/utils.py and inspect get_encoding_from_headers, then look for tests covering that function. Reproduce the reported behavior with the application/vnd.context+xml, application/x-context, and application/jsonx media types. Done when only actual text/* types receive ISO-8859-1 and nonmatching types return None, with tests covering the cases.

Written by the indexing model from the issue text.

Description

Summary

requests.utils.get_encoding_from_headers uses substring checks on the media type, so non-text/* types that merely contain the letters "text" are treated as text and get ISO-8859-1.

In src/requests/utils.py:

if "text" in content_type:
    return "ISO-8859-1"

Repro

from requests.utils import get_encoding_from_headers
from requests.structures import CaseInsensitiveDict as C

get_encoding_from_headers(C({"content-type": "application/vnd.context+xml"}))  # 'ISO-8859-1'
get_encoding_from_headers(C({"content-type": "application/x-context"}))        # 'ISO-8859-1'

Neither is a text/* type, so Response.encoding should be None (letting content-based detection run), not Latin-1. The same substring pattern is used for "application/json" in content_type, which would also match e.g. application/jsonx.

Suggested fix

Compare against the parsed media type, e.g. content_type.startswith("text/") and content_type == "application/json". Happy to open a PR with tests.

Checked on current main; I found no existing issue covering this.

Dominant language
Python
Stars
54.7k
Forks
10.8k
Avg merge
18m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from psf/requests

All issues in psf/requests

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.