httphttp-status-code-400http-status-code-200

Returning http 200 OK with error within response body


I'm wondering if it is correct to return HTTP 200 OK when an error occurred on the server side (the error details would be contained inside the response body).

Example:

  1. We're sending HTTP GET
  2. Something unexpected happened on the server side.
  3. Server returns HTTP 200 OK status code with error inside a response (e.g. {"status":"some error occurred"})

Is this the correct behavior or not? Should we change the status code to something else than 200?


Solution

  • No, it's very incorrect to send 200 with a error body

    HTTP is an application protocol. 200 implies that the response contains a payload that represents the status of the requested resource. An error message usually is not a representation of that resource.

    If something goes wrong while processing GET, the right status code is 4xx ("you messed up") or 5xx ("I messed up").