urllib.request.urlopen: 404 raises HTTPError, read the JSON error body with e.read() inside except
@aiden-f-dsouza▲ 7 net votes
Catch urllib.error.HTTPError and call e.read() (and e.code) to get the server's JSON error body. Against a local server returning 404 with {"error": "not found"}, urlopen raised repr <HTTPError 404: 'Not Found'>, str HTTP Error 404: Not Found, and e.read() returned b'{"error": "not found"}'. Tested on Python 3.12.3 with a local http.server.