[New-bugs-announce] [issue12455] urllib2 Request() forces capitalize() on header names, breaking some requests

Cal Leeming report at bugs.python.org
Thu Jun 30 21:23:38 CEST 2011


New submission from Cal Leeming <cal.leeming at simplicitymedialtd.co.uk>:

I came up against a problem today whilst trying to submit a request to a remote API. The header needed to contain:

'Content-MD5' : "md5here"

But the urllib2 Request() forces capitalize() on all header names, and transformed it into "Content-Md5", which in turn made the remote web server ignore the header and break the request (as the remote side is case sensitive, of which we don't have any control over).

I attempted to get smart by using the following patch:
class _str(str):
    def capitalize(s):
        print s
        return s

_headers = {_str("Content-MD5") : 'md5here'}

But this failed to work:


---HEADERS---
{'Content-MD5': 'nts0yj7AdzJALyNOxafDyA=='}

---URLLIB2 DEBUG---
send: 'POST /api/v1 m HTTP/1.1\r\nContent-Md5: nts0yj7AdzJALyNOxafDyA==\r\n\r\n\r\n'

Upon inspecting the urllib2.py source, I found 3 references to capitalize() which seem to cause this problem, but it seems impossible to monkey patch, nor fix without forking.

Therefore, I'd like to +1 a feature request to have an extra option at the time of the request being opened, to bypass the capitalize() on header names (maybe, header_keep_original = True or something). 

And, if anyone could suggest a possible monkey patch (which doesn't involve forking huge chunks of code), that'd be good too :)

Thanks

Cal

----------
components: Library (Lib)
messages: 139512
nosy: Cal.Leeming
priority: normal
severity: normal
status: open
title: urllib2 Request() forces capitalize() on header names, breaking some requests
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12455>
_______________________________________


More information about the New-bugs-announce mailing list