[New-bugs-announce] [issue14144] urllib2 HTTPRedirectHandler not handling POST data in redirect

Jay Deiman report at bugs.python.org
Mon Feb 27 23:44:09 CET 2012

New submission from Jay Deiman <admin at splitstreams.com>:

I've noticed that urllib2's HTTPRedirectHandler does not redirect a POST request with the POST data.  

If you send a POST request to a server with data, the data is dropped when the new Request is made to the new url.  As stated in a comment in the library itself, redirecting a POST request is not strictly RFC compliant, but it's generally supported anyway.  The problem here being that our POST data is not also being redirected.  I ran into this issue when writing a web api wrapper in Python.

I'm submitting a small patch that fixes this issue:

--- /usr/lib/python2.7/urllib2.py	2011-10-04 16:07:28.000000000 -0500
+++ urllib2.py	2012-02-27 16:03:36.000000000 -0600
@@ -551,7 +551,11 @@
             newheaders = dict((k,v) for k,v in req.headers.items()
                               if k.lower() not in ("content-length", "content-type")
+            data = None
+            if req.has_data():
+                data = req.get_data()
             return Request(newurl,
+                           data=data,

components: Extension Modules
files: urllib2.py.patch
keywords: patch
messages: 154516
nosy: crustymonkey
priority: normal
severity: normal
status: open
title: urllib2 HTTPRedirectHandler not handling POST data in redirect
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file24665/urllib2.py.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list