Skip to content

Include header name in encoding error message - #3767

Open
worksbyfriday wants to merge 1 commit into
encode:masterfrom
worksbyfriday:fix-header-encoding-error-message
Open

worksbyfriday wants to merge 1 commit into
encode:masterfrom
worksbyfriday:fix-header-encoding-error-message

Conversation

@worksbyfriday

Copy link
Copy Markdown

Summary

  • When a header value contains non-ASCII characters that can't be encoded, the UnicodeEncodeError now includes the header name
  • Before: 'ascii' codec can't encode characters in position 0-27: ordinal not in range(128)
  • After: 'ascii' codec can't encode characters in position 0-27: ordinal not in range(128) (header: 'auth')

Fixes #3400

Test plan

  • Added test_header_encoding_error_includes_name verifying error message contains header name
  • All 28 header model tests pass

When a header value contains non-ASCII characters that can't be encoded,
the error message now includes the header name, making it easier to
identify which header is causing the issue.

Fixes encode#3400

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread httpx/_models.py
return key if isinstance(key, bytes) else key.encode(encoding or "ascii")


def _normalize_header_value(value: str | bytes, encoding: str | None = None) -> bytes:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new error context is useful, but it appears to cover only header initialization.

Headers.__setitem__() still encodes values directly:

set_value = value.encode(self._encoding or "utf-8")

For example:

headers = httpx.Headers(encoding="ascii")
headers["X-Test"] = "café"

This still raises a UnicodeEncodeError without identifying X-Test, whereas constructing the same header through Headers({"X-Test": "café"}, encoding="ascii") now includes the name.

Could we route __setitem__() through _normalize_header_value(..., name=key) as well, or otherwise include the header name when assignment fails?

A regression test covering both initialization and assignment with an explicit ASCII encoding would ensure consistent diagnostics.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Mention header name error message for invalid header encodings

2 participants