Mention header name error message for invalid header encodings
Open
Beginner friendly
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 78
- Avg merge
- 8h 59m
- Merged PRs (30d)
- 24
Description
Originally opened by
@RobertCraigieon 2024-11-12 12:40:14 in encode/httpx
Discussed in https://github.com/encode/httpx/discussions/3399
Originally posted by RobertCraigie November 12, 2024
This openai-python user ran into a confusing error when passing a non-ascii header value, would it be possible to mention the header name in the error message?
Minimal repro
import httpx
httpx.Headers({"auth": "здравейздравейздравейздравей"})
Traceback (most recent call last):
File "script.py", line 3, in <module>
httpx.Headers({"auth": "здравейздравейздравейздравей"})
File ".venv/lib/python3.9/site-packages/httpx/_models.py", line 74, in __init__
self._list = [
File ".venv/lib/python3.9/site-packages/httpx/_models.py", line 78, in <listcomp>
normalize_header_value(v, encoding),
File ".venv/lib/python3.9/site-packages/httpx/_utils.py", line 53, in normalize_header_value
return value.encode(encoding or "ascii")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-27: ordinal not in range(128)
```</div>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the header construction in _models.py and the normalize_header_value function in _utils.py, both named in the traceback. Reproduce the non-ASCII header example, then verify that the resulting encoding error identifies the header name while preserving the existing validation behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 72/100