What http status should I send for a malformed json response?

886 Views Asked by At

I'm building a simple http server in nodejs that validates the outgoing json. If the validation fails, what would be the correct status code / error message to send to the client?

2

There are 2 best solutions below

0
CertainPerformance On

Since the error occurs on the server, the error should be in the 500s.

There doesn't appear to be any error message corresponding to this problem in particular, so you can use the generic one indicating that there was some problem on the server: Error 500 Internal Server Error. Logic performed on the server failed to fulfill expectations.

0
reivaj96 On

The common status codes are:

4xx means client errors

  1. 400 bad request
  2. 401 unauthorized
  3. 403 Forbidden
  4. 404 Not found
  5. 405 method not allowed

2xx means HTTP code status

  1. 200 OKs, this is for the update or get resource successfully
  2. 201, means that any source was created
  3. 204, means that request doest have a useful load, but we have used it when are updating

5xx means server errors

  1. 500 Internal server error
  2. 501 Not implemented
  3. 502 bad gateway
  4. 503 services unavailable

There are more. But these are perhaps the most common