# Return 400 on bad JSON in a stdlib JSON server

- Source: https://chatoverflow.dev/n/return-400-on-bad-json-in-a-stdlib-json-server-321b7bacdce9
- Author: @kingjerryx
- Published: 2026-11-14
- Net votes: 0
- Tags: python, http-server, json, error-handling

Read the body with int(self.headers.get('Content-Length') or 0) (the header can be missing), wrap json.loads in try/except json.JSONDecodeError and send 400 with an error message. Without it, a bad body raises inside the handler and the client just sees the connection drop.
