0

I am trying to see a redirect for pricipal.com. When using the developer tools I can see that the redirect fails with status code 400. However, when using python I see Status Code: 200

This is the code I'm using:

import requests
import urllib.request, urllib.error
import vertica_python

r = requests.get('http://pricipal.com')
t = requests.ReadTimeout()
try:    
    print('#################')
# d = geturl(r)
# print(r.status_code)
    print("{}: {}".format(r, r.status_code))
    print('URL: ' + r.url)
    print("{}: {}".format('History: ', r.history))
    print("{}: {}".format(r, r.status_code))
    print('#################')
except requests.ConnectionError:
# prints the int of the status code. Find more at httpstatusrappers.com :)
    print('######FAIL#######')
    print(r + ": failed to connect")
    print(r.ConnectionError)
    print(r.url)
    print('Read timeout: ' + t)
    print('#################')
    print('done')

How can I modify the code to properly display that the redirect has status code 400 and not 200?

Thank you!

opamp
  • 91
  • 9
  • Do you have an ad blocker running? If not, copy the full request headers through your browser's developer tools. – Blender Oct 26 '16 at 19:38
  • @Blender I'm sorry, I'm not too sure what you mean. I just started using the developer tools. Do you just wan the string? – opamp Oct 26 '16 at 19:48
  • On Chrome, it's something like "Copy as cURL command line" when you right click on the request. – Blender Oct 26 '16 at 19:49

1 Answers1

0

By default, requests follows redirects transparently. To get the initial response without the redirect, you can either examine r.history, or disable redirects like this:

import requests
r = requests.get('http://principal.com', allow_redirects=False)
print(r)
MicahStetson
  • 594
  • 3
  • 6
  • Thanks @MicahStetson! However, I still don't see status 400. This time its 302 and history seems to empty. – opamp Oct 26 '16 at 20:00