From: David Bonner Date: Sun, 23 Dec 2012 05:51:26 +0000 (-0500) Subject: fix POST redirects X-Git-Tag: v1.0.4~17^2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7d085b188cbf71d2ba9a3124d7acfc57c75fe954;p=services%2Fpython-requests.git fix POST redirects the redirect handling logic compares then method to upper-case strings, so make sure the method gets upper-cased as well. add a test to POST to /status/302 on httpbin, which fails against httpbin.org right now. i'm submitting a pull request over there to fix that right after this one. once that's accepted, the new test verifies that the fix works. --- diff --git a/AUTHORS.rst b/AUTHORS.rst index bf79ef4..acc78e8 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -117,3 +117,4 @@ Patches and Suggestions - Stephen Zhuang (everbird) - Martijn Pieters - Jonatan Heyman +- David Bonner @rascalking diff --git a/requests/sessions.py b/requests/sessions.py index 982e96f..5947038 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -94,16 +94,13 @@ class SessionRedirectMixin(object): url = urljoin(resp.url, requote_uri(url)) # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4 - if resp.status_code is codes.see_other: + if resp.status_code is codes.see_other and req.method != 'HEAD': method = 'GET' # Do what the browsers do, despite standards... if resp.status_code in (codes.moved, codes.found) and req.method == 'POST': method = 'GET' - if (resp.status_code == 303) and req.method != 'HEAD': - method = 'GET' - # Remove the cookie headers that were sent. headers = req.headers try: @@ -245,7 +242,7 @@ class Session(SessionRedirectMixin): # Create the Request. req = Request() - req.method = method + req.method = method.upper() req.url = url req.headers = headers req.files = files diff --git a/test_requests.py b/test_requests.py index e55a4cf..84d4e8b 100644 --- a/test_requests.py +++ b/test_requests.py @@ -86,6 +86,10 @@ class RequestsTestCase(unittest.TestCase): r = requests.get(httpbin('redirect', '1')) self.assertEqual(r.status_code, 200) + def test_HTTP_302_ALLOW_REDIRECT_POST(self): + r = requests.post(httpbin('status', '302'), data={'some': 'data'}) + self.assertEqual(r.status_code, 200) + def test_HTTP_200_OK_GET_WITH_PARAMS(self): heads = {'User-agent': 'Mozilla/5.0'}