Skip to content

Instantly share code, notes, and snippets.

@wesinator
Last active May 12, 2020 17:37
Show Gist options
  • Save wesinator/e6a0954e5afc637c378882515aec0151 to your computer and use it in GitHub Desktop.
Save wesinator/e6a0954e5afc637c378882515aec0151 to your computer and use it in GitHub Desktop.
Demonstrate requests .json() JSONDecodeError on empty or invalid input
import requests, simplejson
resp_blank = "https://httpbin.org/status/200"
resp_200 = "http://httpstat.us/200"
try:
requests.get(resp_blank).json()
except simplejson.JSONDecodeError as e:
print(e)
try:
requests.get(resp_200).json()
except ValueError as e: # equivalent
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment