shaare.it

Non-ASCII characters in headers

7 Dec 2025

1 min read

Non-ASCII headers

$ python -c "import requests; requests.get('https://example.com', headers={'X-Name':'José\u0000'})"
Traceback (most recent call last):
  ...
ValueError: Invalid header value

Why this happens

Headers must be ASCII and cannot contain control characters. Some servers allow RFC 5987 encoding, but \u0000 and others are invalid.

Fix

  • Restrict headers to printable ASCII or use proper encoding schemes.

Wrong code

import requests
requests.get("https://example.com", headers={"X-Name": "Jos\u0000e"})

Fixed code

import requests
requests.get("https://example.com", headers={"X-Name": "Jose"})