HTTP codes? Looks Easy, but ain’t.
Working with application or web development means interacting with HTTP responses.
So, what actually are HTTP codes?
Whenever there is a request from the client to the server, the server sends an HTTP status code along with the response, to indicate the status of the response received or we can say what happened because of the request we sent - did it create any new data, retrieve any new data or is there an error or a glitch!!!!
Most of us are familiar with 200, 404, 500 errors in common sight but when to use them is the point. To easily memorize the error codes through mnemonics check this link - https://http.cat/
Here each series is defined by its first digit (1xx, 2xx, 3xx, 4xx, 5xx) and has its own purpose and meaning in the HTTP world.
- 1xx (Informational): Request initiated by the browser is continuing.
- 2xx (Successful): The request was successfully received, understood, and processed by the server.
- 3xx (Redirection): Requested information that is no longer at the provided address.
- 4xx (Client Error): When there is a problem with the request of the client.
- 5xx (Server Error): When a client made a valid request, but the server is unable to complete the transfer due to any platform issues.
Similarly, in each series for eg. 200, 201 or 204, each has its own meaning in success and 400, 401, 403, 404, 409 has its own meaning in what went wrong.
Any of us can return any status code when developing any API but using appropriate codes is not just a norm but a necessity.
For eg. When you are creating an entity in the system you might send a 500 HTTP code along with the data you want to send back. Funny right? This will work in Postman or through CURL but will result in unintentional behavior when consuming this API in JavaScript. This is because when promises are fulfilled JavaScript, it automatically catches this 500 HTTP code in the error block and raises an error in the browser. Hence, to make systems understand the response properly, the use of proper status codes is mandatory for systems to interact perfectly.
DevOps Engineering Lead at Redux
5 年Good article...
CEO @ Cosmocloud | Ex-LinkedIn | Angel Investor | MongoDB Champion | Book Author | Patent Holder (Distributed Algorithms)
5 年Amazing blog..!