Skip to content

Commit

Permalink
#3490 removing Content-Type and Transfer-Encoding headers on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Aug 10, 2016
1 parent e4b7040 commit 20a2b5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

Expand Down

0 comments on commit 20a2b5a

Please sign in to comment.