简体   繁体   中英

python requests library .json() + django 500

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM