Why JSON has a status property

I came across a practice that I found quite common. I even found the web page that gave this name, but I forgot the name and can no longer find this page in google.

The practice is that every JSON response from a REST service should have the following structure:

{
    "status": "ok",
    "data": { ... }
}

or in case of an error:

{
    "status": "error",
    "message": "Something went wrong"
}

My question is: what is the meaning of this status property in JSON? In my opinion, this is what HTTP status codes were created for.

REST uses HTTP means of communication between the client and the server, for example, to delete, use the verb "DELETE". In the same way, 404 should be used if the resource is not found, etc. Therefore, in accordance with this thinking, any errors must be correctly encoded in HTTP status.

HTTP 200 JSON? , .

, "", URL. HTTP, "", .

:

  • ( HTTP- HTTP- )
  • , , , HTTP , , HTTP , JSON .
+5
3

:

  • HTTP () . HTTP, , , , . HTTP- (, XML, JSON ..), . (, ) .

  • JSON , , status, JSON, , , , .

, , 200 "status": "error" JSON.

+3

. , , , , REST . REST. REST ; , webapps REST-ful.

, 200, , -. HTTP - .

+4

HTTP status codes can be triggered by a variety of things, including load balancers, proxies, caches, firewalls, etc. None of this will change your JSON output unless they completely break it, which can also be considered an error.

Bottom line: it is more reliable to do this through JSON.

+2
source

All Articles