A work in progress
When building your RESTful API, one of the easiest ways to know if the process completed as expected or not is the HTTP status codes returned by the server. That’s why I always use the HTTP status codes for testing, before getting into more elaborate testing of the server response. Underneath you’ll find a list of the most common status code followed by a discussion of variations in relation to different HTTP request methods.
GET
Success:
- 200 “OK”
Invalid request:
- 404 “Not found”
POST
Success:
- 200 “OK”
Invalid request:
- 404 “Not found”
PUT
Success:
- 200 “OK”,
- 201 “Created”
Invalid request:
- 404 “Not found”
DELETE
Success:
- 200 “OK”,
- 204 “No content”
Invalid request:
- 404 “Not found”
Variations
200 “OK”
201 “Created”
204 “No content”
- When using this with express.js as I do, be aware, that express also “mutes” your response message. That is, if you try to return the status code with a specific response message, the message will not be received. I spent quite some time trying to figure out why my test of the response text did not match the expected value. But I guess it makes sense, that if the status code communicates “No content”, no content is being sent back in response. Be aware though, that this status code communicates, that the headers can hold useful information, that is used in the process. For that reason, it is not entirely “quiet”, when choosing the 204 status code.
400 “Bad request” vs 404 “Not found”
More reading