Enforce JSON Content-Type and method early in handlers

9200
0

A lot of handler complexity disappears if you reject bad requests early. I enforce the HTTP method (POST, PUT, etc.) and require Content-Type: application/json before attempting to decode. This prevents confusing errors where clients send form-encoded payloads or forget headers and then get a generic 400. I also keep the check tolerant of charset parameters (application/json; charset=utf-8). In production, this improves observability because you can return a specific error code like UNSUPPORTED_MEDIA_TYPE rather than a generic “bad request.” It also helps security: you avoid accidentally parsing unexpected formats. Combined with http.MaxBytesReader and strict JSON decoding, this becomes a clean perimeter for request parsing.