Search code examples
pythonpython-3.xposthttpspython-requests

Python requests POST command is not following 302 redirect


When passing a POST request to a website via requests, it is failing to complete and will wait indefinitely.

I have reviewed the behaviour in Fiddler and I can see that the POST request is returning successfully with a 302 redirect. However, Requests continues to wait and will never continue although the packets have returned.

I'm stuck on how to diagnose this as I get no solid error and the packets appear to return, any help will be much appreciated.

The post request is built as follows:

loginData = 'data=45B892A0F9C127FB0A052CB&cid=1000'
s.post(url, verify=rVerify, headers=headers, data=loginData)

I have tried allow_redirects as both True and False with no success.

Here is the request and response in Fiddler:

fiddler result

Update header details:

'User-Agent': 'Mozilla/5.0'
'Content-Type': 'application/x-www-form-urlencoded'
'Connection': 'Keep-Alive'
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
'Accept-Language': 'en-US,en;q=0.5'
'Accept-Encoding': 'gzip, deflate, br'

Update:

  • I have tested just using the URL and the command will still hang.
  • I have also attempted a GET request and this also hangs.

Solution

  • So the problem appears to be with Python3 and the httplib libary not being able to parse the header returned from the server.

    Here is the the issue: https://github.com/kennethreitz/requests/issues/3098

    To get around this I have just changed to Python 2 which doesnt need to parse the header in the same way.