New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json() crashes interpreter with a JSONDecodeError #4291
Comments
Hey @justlurking, this is actually working as expected. The .json() method is only usable on Response content that is in the json format. The webpage you're requesting is returning an HTML document and therefore won't work. You'll need to either modify your Accept headers and attempt to retrieve valid json data, or add code to handle the case of json data not being readily available. I'm closing this since things are working as expected. |
Thanks for the super quick and informative reply, and apologies for wasting your time. I should have realized it might be something like that given the multiple versions I tested and lack of outcry on GitHub. 🤦 I imagine the best thing to do would be to import json.JSONDecodeError and do something like this?
|
So this should actually be a subclass of ValueError so I think you'll be able to catch that if you don't want to import. It's up to you on the level of specificity you'd like. That general idea is the approach I'd suggest though. Hope that helps! |
I have reproduced this with requests v. 2.18.4 and 2.14.2, along with Python 3.6.1 and 3.6.2. I have used my system-installed Python/requests (macOS Sierra with Python 3.6.1 downloaded from python.org), Python / requests installed as part of conda 4.3.25, and the latest Python and requests installed from conda-forge.
Same results everywhere.
Expected Result
Expected to see a dictionary of JSON values, as indicated here: http://docs.python-requests.org/en/master/
Actual Result
Python interpreter crashes.
Reproduction Steps
System Information
The text was updated successfully, but these errors were encountered: