python requests library .json() + django 500 - python

When I use the requests library with django and I get a 500 error back. response.json() gives me this error:
response = requests.post(....)
print("-----------------------block found!!!-----------------------")
print(response.status_code)
print(response.json())
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
Is there a way to represent a django 500 response with the requests library in a readable manner?

Assuming you get an HTTP 500 response, You would definitely receive an empty source, Which prevents the .json() function from taking place.
Why not write an exception clause to handle the exceptions, like below:
try:
response = requests.post(....)
print("-----------------------block found!!!-----------------------")
print(response.status_code)
print(response.json())
except HTTPError as e:
print('Error has occurred: ', e.response.status_code)

Related

Python requests keeps returning 401 client error

I have some python test code that is requesting web pages from a embedded controller. For general .htm pages it works but when I request a .txt file stored on it I get a
"401 Client Error: Unauthorized for url: http://192.168.61.30/fs/work/sys/crashdump/log0.txt"
HTTP error
The code is as follows:-
for url in ['http://192.168.61.30/get_version.htm']:
try:
cookies = {'Auth': access_token}
response = requests.get(url, cookies=cookies)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print("*****Access obtained******")
print(response.content);
print('Success!')
for url in ['http://192.168.61.30/fs/work/sys/crashdump/log0.txt']:
try:
cookies = {'Auth': access_token}
response = requests.get(url, cookies=cookies)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print("*****Access obtained******")
print(response.content);
print('Success!')
The 1st one works fine, however the 2nd one always fails with 401 client error and as far as I can work out never gets as far as the web server. Looking at the returned headers I don't see any authorisation requests. I have also disabled the proxies.
If I send the same request via a web browser I get no problems
The headers response is
{'Server': 'HPCi Controller Web server', 'Connection': 'close', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Type': 'text/html'}
I have tried adding WWW-Authentication: Basic to the headers, but no success

Cannot get json response from http request

I am trying to get a JSON response using the requests module. Was wondering if anyone knows what could be causing this.
import requests
url = "https://www.google.com/"
data = requests.get(url)
data.json()
Error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
From the docs:
In case the JSON decoding fails, r.json() raises an exception. For
example, if the response gets a 204 (No Content), or if the response
contains invalid JSON, attempting r.json() raises ValueError: No JSON
object could be decoded.
You need to have a url that could possibly return a json:
import requests
url = 'https://github.com/timeline.json'
data = requests.get(url).json()
print(data)
OUTPUT:
{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}
The page you are returning is not json, its html

How to use exceptions for different cases with python requests

I have this code
try:
response = requests.post(url, data=json.dumps(payload))
except (ConnectionError, HTTPError):
msg = "Connection problem"
raise Exception(msg)
Now i want the following
if status_code == 401
login() and then try request again
if status_code == 400
then send respose as normal
if status_code == 500
Then server problem , try the request again and if not successful raise EXception
Now these are status codes , i donn't know how can i mix status codes with exceptions. I also don't know what codes will be covered under HttpError
requests has a call called raise_for_status available in your request object which will raise an HTTPError exception if any code is returned in the 400 to 500 range inclusive.
Documentation for raise_for_status is here
So, what you can do, is after you make your call:
response = requests.post(url, data=json.dumps(payload))
You make a call for raise_for_status as
response.raise_for_status()
Now, you are already catching this exception, which is great, so all you have to do is check to see which status code you have in your error. This is available to you in two ways. You can get it from your exception object, or from the request object. Here is the example for this:
from requests import get
from requests.exceptions import HTTPError
try:
r = get('http://google.com/asdf')
r.raise_for_status()
except HTTPError as e:
# Get your code from the exception object like this
print(e.response.status_code)
# Or you can get the code which will be available from r.status_code
print(r.status_code)
So, with the above in mind, you can now use the status codes in your conditional statements
https://docs.python.org/2/library/urllib2.html#urllib2.URLError
code
An HTTP status code as defined in RFC 2616. This numeric value
corresponds to a value found in the dictionary of codes as found in
BaseHTTPServer.BaseHTTPRequestHandler.responses.
You can get the error code from an HTTPError from its code member, like so
try:
# ...
except HTTPError as ex:
status_code = ex.code

requests.HTTPError uncaught after a requests.get() 404 response

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?
Interpreter is your friend:
import requests
requests.get('google.com/admin')
# MissingSchema: Invalid URL u'google.com/admin': No schema supplied
Also, requests exceptions:
import requests.exceptions
dir(requests.exceptions)
Also notice that by default requests doesn't raise exception if status is not 200:
In [9]: requests.get('https://google.com/admin')
Out[9]: <Response [503]>
There is raise_for_status() method that does it:
In [10]: resp = requests.get('https://google.com/admin')
In [11]: resp
Out[11]: <Response [503]>
In [12]: resp.raise_for_status()
...
HTTPError: 503 Server Error: Service Unavailable
Running your code in python 2.7.5:
import requests
try:
response = requests.get('google.com/admin') #Should return 404
except requests.HTTPError, e:
print 'HTTP ERROR %s occured' % e.code
print e
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:
except (requests.exceptions.MissingSchema) as e:
print 'Missing schema occured. status'
print e
Note also it is not a missing schema but a missing scheme.

Getting error headers with urllib2

I need to send a PUT request to a web service and get some data out of error headers that is the expected result of the request. The code goes like this:
Request = urllib2.Request(destination_url, headers=headers)
Request.get_method = lambda: 'PUT'
try:
Response = urllib2.urlopen(Request)
except urllib2.HTTPError, e:
print 'Error code: ', e.code
print e.read()
I get Error 308 but response is empty and I'm not getting any data out of HTTPError. Is there a way to get HTTP headers while getting an HTTP error?
e has undocumented headers and hdrs properties that contains the HTTP headers sent by the server.
By the way, 308 is not a valid HTTP status code.

Categories

Resources