Should the status field on the output of a REST API call be a numeric code or a string?
I have a REST-ish API that returns JSON. One of the output fields is status
that indicates the success of the call. I understand that it is somewhat standard to use numeric status codes such as
200 success
400 syntax error
401 authentication error
402 general error
404 user not found
408 timed out
500 fatal error
501 not yet implemented
Is there any reason (other than transferring slightly fewer bytes) for using numeric codes rather than more descriptive characters like status, for example
success
syntax_error
auth_error
general_error
user_not_found_error
time_out_error
fatal_error
not_yet_implemented_error
It would be helpful to point out modern use cases in respected APIs.
source to share
You can make them however you want, as long as they are standard (through your application) and documented for the benefit of those using the web service.
The tricky part of using HTTP status codes such as 404 is that a real REST web service is based on the HTTP transport, so it makes sense to use recognized standards.
However, you may find that the HTTP / 1.1 status codes do not match - for example, your "404 user not found" is equivalent to "404 not found" - as long as the meaning is fine, I think.
If you need to create entirely new codes, stick to the HTTP groups - 2xx for acceptable results, 3xx for necessary changes, 4xx for client errors, and 5xx for server-side errors.
source to share