From 20a2b5ac71f1d6ce674ca2633172ef5964ec9c38 Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Wed, 10 Aug 2016 11:02:26 -0600 Subject: [PATCH] #3490 removing Content-Type and Transfer-Encoding headers on redirect --- requests/sessions.py | 7 +++++-- tests/test_requests.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index 8d8d9105b8..eafdb0b810 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -140,8 +140,11 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None, # https://github.com/kennethreitz/requests/issues/1084 if resp.status_code not in (codes.temporary_redirect, codes.permanent_redirect): - if 'Content-Length' in prepared_request.headers: - del prepared_request.headers['Content-Length'] + # https://github.com/kennethreitz/requests/issues/3490 + purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding') + for header in purged_headers: + if header in prepared_request.headers: + del prepared_request.headers[header] prepared_request.body = None diff --git a/tests/test_requests.py b/tests/test_requests.py index efbc7ed0bd..d1ddf393e8 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -221,6 +221,24 @@ def test_http_303_doesnt_change_head_to_get(self, httpbin): assert r.history[0].status_code == 303 assert r.history[0].is_redirect + def test_header_removal_on_redirect(self, httpbin): + purged_headers = ('Content-Length', 'Content-Type', 'Transfer-Encoding') + data = {'test': 'data'} + ses = requests.Session() + req = requests.Request('POST', httpbin('post'), data=data) + prep = ses.prepare_request(req) + resp = ses.send(prep) + + # Mimic a redirect response + resp.status_code = 302 + resp.headers['location'] = 'get' + + # Run request through resolve_redirects + next_resp = next(ses.resolve_redirects(resp, prep)) + assert next_resp.request.body == None + for header in purged_headers: + assert header not in next_resp.request.headers + def test_HTTP_200_OK_GET_WITH_PARAMS(self, httpbin): heads = {'User-agent': 'Mozilla/5.0'}