httpx icon indicating copy to clipboard operation
httpx copied to clipboard

Include header name in encoding error messages

Open suojae opened this issue 2 months ago • 0 comments

Summary

When headers contain non-ASCII characters, the error message now includes the header name to make debugging easier.


Before:

httpx.Headers({"Authorization": "Bearer valid", "X-Custom": "안녕"})
# UnicodeEncodeError: 'ascii' codec can't encode characters...
# Which header failed? 

After:

  httpx.Headers({"Authorization": "Bearer valid", "X-Custom": "안녕"})
  # UnicodeEncodeError: Header 'X-Custom' value contains non-ASCII characters
  # Clear which header is the problem 

Changes

  • Added optional header_name parameter to _normalize_header_key and _normalize_header_value
  • Updated error messages to include header name when encoding fails
  • Added tests for both header key and value encoding errors

related #3400

suojae avatar Oct 25 '25 12:10 suojae