urllib2 header casing discrepancy

Darren Spruell phatbuckett at gmail.com
Wed Feb 23 17:48:48 EST 2011


Spent some time tripping today over HTTP headers being added to the
Request object in "proper" header case (all components title cased)
but finding that the resulting header dictionary object had the
headers keyed with first component only capitalized.

## http://paste.pound-python.org/show/3273/

>>> import urllib2
>>> req = urllib2.Request("http://www.google.com")
>>> req.add_header('User-Agent', 'bob')
>>> req.add_header('X-Poopy', 'Big Poopy')
>>> req.get_header('User-Agent')
>>> req.get_header('X-Poopy')
>>> req.headers
{'X-poopy': 'Big Poopy', 'User-agent': 'bob'}
>>> req.get_header('User-agent')
'bob'
>>> req.get_header('X-poopy')
'Big Poopy'

I'd expect the header put in as "User-Agent" to be stored and
retrieved using the same case but this is clearly not the case. HTTP
header names are case-insensitive so it matters not how they're sent
but programmatically  it would seem like we'd want to ensure that the
specified input is preserved.

http://www.cs.tut.fi/~jkorpela/http.html
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2

This is observed under Python 2.6.1 and 2.6.6 on OS X and Linux.

Am I misunderstanding how to use these functions? Do I need to adjust
my expectations? :)

-- 
Darren Spruell
phatbuckett at gmail.com



More information about the Python-list mailing list