I'm having a slight problem with the requests library.
Say for example I have a statement like this in Python:
try:
request = requests.get('google.com/admin') #Should return 404
except requests.HTTPError, e:
print 'HTTP ERROR %s occured' % e.code
For some reason the exception is not being caught. I've checked the API documentation for requests but it's a bit slim. Is there anyone who has more experience with the library that might be able to help me out?
Running your code in python 2.7.5:
Results in:
File "C:\Python27\lib\site-packages\requests\models.py", line 291, in prepare_url raise MissingSchema("Invalid URL %r: No schema supplied" % url) requests.exceptions.MissingSchema: Invalid URL u'google.com/admin': No schema supplied
To get your code to pick up this exception you need to add:
Note also it is not a missing schema but a missing scheme.
Interpreter is your friend:
Also,
requests
exceptions:Also notice that by default
requests
doesn't raise exception if status is not200
:There is raise_for_status() method that does it: