HTTP Header Capitalization in urllib.request.AbstractHTTPHandler (Python 3.3)

Chris Angelico rosuav at gmail.com
Tue Nov 19 08:11:04 CET 2013


On Tue, Nov 19, 2013 at 1:25 AM, Logan Owen <logan at s1network.com> wrote:
> Hello everyone,
>
> I was hoping for some advice in dealing with an edge case related to
> Python's HTTP Header handling.  Python is correctly assuming that the
> HTTP header field name (eg Content-Type) is case insensitive, but I have
> a webservice I am interacting with that does not follow the standards.
> I am passing in the header "SOAPAction" and do_request of
> AbstractHTTPHandler is applying title() to the string, leading to the
> field name being "Soapaction", which the server does not recognize.
>
> So my question is, what does everyone suggest is the best solution for
> being able to pass a case sensitive header to the server?

So what you're asking is: How can Python be made less standards-compliant?

Well! You may be in luck. The evil part of my brain has just gone
digging for a solution. According to cpython/Lib/urllib/request.py
from Python 3.4 alpha (unlikely to be different in 3.3), the code
you're looking for is either line 1193ish:

        for name, value in self.parent.addheaders:
            name = name.capitalize()
            if not request.has_header(name):
                request.add_unredirected_header(name, value)

or line 1226:
        headers = dict((name.title(), val) for name, val in headers.items())

I'm assuming you're either stuffing stuff into addheaders or passing
it headers. Well, dere's nuffink says it has to be a string...

class SOAPAction:
    def capitalize(self):
        return "SOAPAction"
    title = capitalize

Just use SOAPAction() instead of "SOAPAction" as your key, and TYAOOYDAO!

ChrisA

[1] "there you are, out of your difficulty at once" - cf WS Gilbert's "Iolanthe"



More information about the Python-list mailing list