Skip to content

Instantly share code, notes, and snippets.

@cbare
Created December 19, 2012 00:05
Show Gist options
  • Select an option

  • Save cbare/4333273 to your computer and use it in GitHub Desktop.

Select an option

Save cbare/4333273 to your computer and use it in GitHub Desktop.
Show how to follow redirects, including rePOSTing, with the python requests library.
## follow redirects, including rePOSTing, with the requests library.
######################################################################
import requests
## authentication with no redirects
ans = requests.post(url=endp_prod + "/session", data=json.dumps(d), headers=h)
## results in a successful login
ans.status_code
201
ans.json
{u'sessionToken': u'Z8T0JFdP88iol3l7XSjj0Q00'}
## POST requests will be turned into GETs, unless we set strict_mode to True
requests.defaults.defaults['strict_mode'] = True
## do the POST, including the flag follow redirects
ans = requests.post(url=endp_dev + "/session", data=json.dumps(d), headers=h, allow_redirects=True)
## yay!
ans.status_code
201
ans.json
{u'sessionToken': u'6cxGKVwTwxujxYFUUsmI1w00'}
@jeorlie
Copy link

jeorlie commented Jul 19, 2021

I tried requests.get(url, allow_redirects=True) but it is not working.

@pghole
Copy link

pghole commented Sep 14, 2021

What does the "allow_redirects" do ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment