What HTTP Status code to respond with

Protocol

In telecommunications, a communications protocol is a system of rules that allow two or more entities of a communications system to transmit information via any kind of variation of a physical quantity. These are the rules or standard that defines the syntax, semantics and synchronization of communication and possible error recovery methods. Protocols may be implemented by hardware, software, or a combination of both.

Source: Wikipedia (https://en.wikipedia.org/wiki/Communications_protocol)

We web developers thrive on HTTP, another protocol. For a long time we were content with GET and POST. Especially in the Microsoft world. Other HTTP verbs were unheard of. Come ASPNET MVC the paradigm changed and we were forced to become "HTTP Aware". With the adoption of REST (REpresentational State Transfer) and ASPNET WebAPI even more so. However the protocol is not just one way communication and the way HTTP Verbs like GET, PUT, POST, DELETE... are meant for the client to tell the server what it needs to do, similarly the server will have to respond in a predictable matter. In HTTP we do this with the HTTP Status codes. 404 anyone? Of course there are request and response headers in the protocol, but lets reserve that for another discussion.

HTTP Status Codes

The first digit of the Status-Code defines the class of response. The last two digits do not have any categorization role. There are 5 values for the first digit
1xx: Informational - Request received, continuing process
2xx: Success - The action was successfully received, understood, and accepted
3xx: Redirection - Further action must be taken in order to complete the request
4xx: Client Error - The request contains bad syntax or cannot be fulfilled
5xx: Server Error - The server failed to fulfill an apparently valid request

Source: IETF (https://tools.ietf.org/html/rfc2616#section-6.1.1)

Picking the right response code however is tricky and is a series of long winding decisions. I am often confused making those decisions and end up making wrong choices. So to keep matters simple I started looking for a simple decision tree to aid in the decision making process. I found quite a few but they were too complex to decipher and scroll through, so decided to bite the bullet and draw one in Visio. The png below is for reference and the VSDX is available here.

For the sake of full disclosure, this is not a ultimate guide. I have omitted the verbs TRACE, used for remote application-layer loopbacks and CONNECT, used by proxies that can dynamically switch to being a tunnel. Both of which I have no business coding as of today or in near future.

The way I am working through this file right now, is when I have a question about a response code, I look up the Verb being requested, and follow the decision tree from there. Hope this helps you too.

alt

Further reading:

PS: Very important response code is also not included in the diagramHTTP 418. Sorry for being a HTTP 418
TeaPot