From 470af31f4a5e9edfae1c818f5104ee83f5f541cb Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 24 Nov 2013 11:09:00 +0000 Subject: [PATCH 1/2] Handle 301s 'properly'. --- requests/sessions.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/requests/sessions.py b/requests/sessions.py index 8aff5913..c0a0e851 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -112,10 +112,16 @@ class SessionRedirectMixin(object): method = 'GET' # Do what the browsers do, despite standards... - if (resp.status_code in (codes.moved, codes.found) and + # First, turn 302s into GETs. + if (resp.status_code == codes.found and method not in ('GET', 'HEAD')): method = 'GET' + # Second, if a POST is responded to with a 301, turn it into a GET. + # This bizarre behaviour is explained in Issue 1704. + if (resp.status_code == codes.moved) and (method == 'POST'): + method = 'GET' + prepared_request.method = method # https://github.com/kennethreitz/requests/issues/1084 From 3369d87da96e38772faa773b755189320ab5b81c Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 24 Nov 2013 11:13:49 +0000 Subject: [PATCH 2/2] Cleanup the redirect if blocks. --- requests/sessions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index c0a0e851..9c95b58d 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -113,13 +113,12 @@ class SessionRedirectMixin(object): # Do what the browsers do, despite standards... # First, turn 302s into GETs. - if (resp.status_code == codes.found and - method not in ('GET', 'HEAD')): + if resp.status_code == codes.found and method != 'HEAD': method = 'GET' # Second, if a POST is responded to with a 301, turn it into a GET. # This bizarre behaviour is explained in Issue 1704. - if (resp.status_code == codes.moved) and (method == 'POST'): + if resp.status_code == codes.moved and method == 'POST': method = 'GET' prepared_request.method = method