urllib2 content-type headers

Miles Kaufmann milesck at umich.edu
Sun Jun 21 15:20:38 EDT 2009


On Jun 21, 2009, at 12:01 PM, TYR wrote:

> 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
> OpenerDirector.open()).
>
> And:
>
> 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"?

Those headers are added (by AbstractHTTPHandler.do_request_) only if  
they are missing.

-Miles




More information about the Python-list mailing list