I'm developing a WebSocket API and I was wondering if there was either a standard or best practice for responding to client messages. For example, if my API is expecting only stringified numbers but receives a word, what's the best way to respond? Is there something analogous to a 400 response? Conversely, is there anything like a 200 response for valid input?
WebSockets – Equivalent to HTTP Status Codes?
apiapi-designwebsockets
Related Solutions
When in doubt, consult the documentation. Reviewing the W3C definitions for HTTP Status codes, gives us this:
200 OK - The request has succeeded. The information returned with the response is dependent on the method used in the request.
404 Not Found - The server has not found anything matching the Request-URI.
In the context of your API, it very much depends on how queries are created and how objects are retrieved. But, my interpretation has always been that:
- If I ask for a particular object, and it exists return
200
code, if it doesn't exist return the correct404
code. - But, if I ask for a set of objects that match a query, a null set is a valid response and I want that returned with a
200
code. The rationale for this is that the query was valid, it succeeded and the query returned nothing.
So in this case you are correct, the service isn't searching for "a specific thing" it is requesting a particular thing, if that thing isn't found say that clearly.
I think Wikipedia puts it best:
200 OK - ... The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource.
404 Not Found - The requested resource could not be found but may be available again in the future. Subsequent requests by the client are permissible.
Seems pretty clear to me.
Regarding the example requests
/GoalTree/GetByDate?versionDate=...
/GoalTree/GetById?versionId=...
For the format, you said, you always return the nearest revision to that date. It will never not return an object, so it should always be returning 200 OK
. Even if this were able to take a date range, and the logic were to return all objects within that timeframe returning 200 OK - 0 Results is ok, as that is what the request was for - the set of things that met that criteria.
However, the latter is different as you are asking for a specific object, presumably unique, with that identity. Returning 200 OK
in this case is wrong as the requested resource doesn't exist and is not found.
Regarding choosing status codes
- 2xx codes Tell a User Agent (UA) that it did the right thing, the request worked. It can keep doing this in the future.
- 3xx codes Tell a UA what you asked probably used to work, but that thing is now elsewhere. In future the UA might consider just going to the redirect.
- 4xx codes Tell a UA it did something wrong, the request it constructed isn't proper and shouldn't try it again, without at least some modification.
- 5xx codes Tell a UA the server is broken somehow. But hey that query could work in the future, so there is no reason not to try it again. (except for 501, which is more of a 400 issue).
You mentioned in a comment using a 5xx code, but your system is working. It was asked a query that doesn't work and needs to communicate that to the UA. No matter how you slice it, this is 4xx territory.
Consider an alien querying our solar system
Alien: Computer, please tell me all planets that humans inhabit.
Computer: 1 result found. Earth
Alien: Computer, please tell me about Earth.
Computer: Earth - Mostly Harmless.
Alien: Computer, please tell me about all planets humans inhabit, outside the asteroid belt.
Computer: 0 results found.
Alien: Computer, please destroy Earth.
Computer: 200 OK.
Alien: Computer, please tell me about Earth.
Computer: 404 - Not Found
Alien: Computer, please tell me all planets that humans inhabit.
Computer: 0 results found.
Alien: Victory for the mighty Irken Empire!
On create, I usually return the ID of the entity I just created. On an update, I return the object I just updated. Delete I usually don't return anything.
In the case of create, the most interesting part is the id of what was created. You throw an error if you couldn't create, so the boolean value you use isn't that interesting. You could also return the entire object with the ID.
On an update, returning the object with any modified fields (either by the updater or someone else) makes the most sense.
Delete either works or you throw an exception, no need for a return value.
Best Answer
Generally, you want your webservice to receive and respond json messages. This is to allow maximum flexibility for later. The addition of new fields doesn't change the underlying types expected on one end or the other.
Expanding on this further, generally you always want the root to be an object, and not an array. This is because arrays can be empty, and again, you want to provide a baseline for input and output that both flexible and will allow for change in the future.
Typically you tend to have a field indicative of status. We're talking about http type response codes, but this could conceivably be any arbitrary status code system that you want.
So you would expect, that if the message is "GREEN", that you would expect the following minimum message in the reply:
So on the receiver end, should you not find valid json or should you not find a status field, it is a red flag! Otherwise, check the status to determine if the operation was a success. 200 here of course being standard http code for saying everything went okay.
Afterwards, you can perform any sort of logic on top of this, such as, if you see status 200, then you should also find a field called "message". Again, the system is arbitrary, but generally you set it up this way to create a baseline for passing messages which lets you establish a basic protocol basis for communication between server and client.