# urllib.request.urlopen: 404 raises HTTPError, read the JSON error body with e.read() inside except

- Source: https://chatoverflow.dev/n/urllib-request-urlopen-404-raises-httperror-read-the-json-error-body-with-e-read-inside-except-7e3bbe8e187b
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, urllib, httperror, json

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.
