kennethreitz/requests — Issue #353: 'allow_redirects' cause a post request to hang
Author: ksamuel
Created: 2012-01-11 11:06:54+00:00
State: open

I tried the following code:

response = requests.post(url, data=request.message,
headers=request.headers,
allow_redirects=True)

But it hangs forever (no error) while this works (I get redirected and got the finale response):

while status_code.startswith('3'):

response = requests.post(url, data=request.message,
headers=request.headers)
status_code = str(response.status_code)
url = response.headers.get('location', '')

More informations:

- Initial Url is http, and it's redirected to https. The adresse remains the same.
- The first request returns nothing but the redirection returns XLM.
- This is tested in ipython only for now.

I digged into the code, and it hangs here, in models.py:

# The inner try .. except re-raises certain exceptions as
# internal exception types; the outer suppresses exceptions
# when safe mode is set.
try:
# Send the request.
r = conn.urlopen(
method=self.method,
url=self.path_url,
body=body,
headers=self.headers,
redirect=False,
assert_same_host=False,
preload_content=False,
decode_content=True,
retries=self.config.get('max_retries', 0),
timeout=self.timeout,
)

Some more info:

# arguments
ipdb> dict( method=self.method, url=self.path_url, body=body, headers=self.headers, redirect=False, assert_same_host=False, preload_content=False, decode_content=True, retries=self.config.get('max_retries', 0), timeout=self.timeout)
{'redirect': False,
'retries': 0,
'url': '/dir/doc.asmx', 'body':
None,
'headers': {'Content-Length': '26337',
'Accept-Encoding': 'identity, deflate, compress, gzip',
'SOAPAction': u'"http://tempuri.org/Method"',
'Content-Type': 'text/xml; charset=utf-8',
'Accept': '*/*',
'User-Agent': 'python-requests/0.9.1'},
'timeout': None,
'preload_content': False,
'assert_same_host': False,
'method': 'POST',
'decode_content': True}

# local variables
ipdb> locals()
{'body': None, 'url': 'https://subdomain.domain.org/dir/doc.asmx',
'ipdb': ,
'self': ,
'anyway': False,
'_p': ParseResult(scheme='https',
netloc='subdomain.domain.org', path='/dir/doc.asmx',
params='',
query='',
fragment=''),
'cert_loc': '/home/me/.virtualenvs/project/local/lib/python2.7/site-packages/certifi/cacert.pem',
'proxy': None,
'content_type': None,
'conn': ,
'prefetch': False}

# platform
ipdb> sys.version
'2.7.2+ (default, Oct 4 2011, 20:06:09) \n[GCC 4.6.1]'

ipdb> requests.__version__
'0.9.1'

I'm on Ubuntu 11.10 64 bits.)