urllib2 content-type headers

TYR a.harrowell at gmail.com
Sun Jun 21 12:01:53 EDT 2009

I have a little application that wants to send data to a Google API.
This API requires an HTTP header to be set as follows:

Authorization: GoogleLogin auth=[value of auth token goes here]

Unfortunately, I'm getting nothing but 400 Bad Requests. I suspect
this is due to an unfeature of urllib2. Notably, although you can use
urllib2.Request's add_header method to append a header, the
documentation (http://docs.python.org/library/urllib2.html) says that:

remember that a few standard headers (Content-Length, Content-Type and
Host) are added when the Request is passed to urlopen() (or


Note that there cannot be more than one header with the same name, and
later calls will overwrite previous calls in case the key collides.

To put it another way, you cannot rely on Content-Type being correct
because whatever you set it to explicitly, urllib2 will silently
change it to something else which may be wrong, and there is no way to
stop it. What happened to "explicit is better than implicit"?

More information about the Python-list mailing list