Decoding HTTP Status Codes

Decoding HTTP Status Codes

Only Resource You Need To Understand HTTP Status Codes

Hello learners!

Ever got an ERROR 404 NOT FOUND while surfing the internet? Ever wondered why 404? What is the meaning behind it?

If the answer to the above questions is YES, then you have clicked upon the right article. This article is capable enough to teach HTTP Status Codes even to a 10 year old kid. You will go through various diagrams, analogies & easy to understand explanations that you will never ever have to go to any other resource to learn about these Status Codes.


Why Do HTTP Status Codes Even Exist?

Let’s arrive at an answer to the above question by analysing a situation.

Analogy → You sent a parcel to your friend through a courier service (server). Obviously you would want to know the status of your parcel whether it has reached your friend or not, whether the parcel delivery has taken too much time, whether the parcel is even allowed to be delivered or not (in case you are sending something suspicious!). All of this is answered to you by the courier service that you have selected through various codes that you receive on your SMS app.

Similarly, server tells the client (browser) about the condition of the request that the client generated through these HTTP Status Codes! These codes help in standardising the communication.

Client is generating a request and server is responding back with HTTP Status Codes to represent the condition of the request after considering it. It can be an approval, denial, timeout etc. All of this will be broken down in this article


Categories of HTTP Status Codes

There are 5 categories of HTTP Status Codes that govern the entirety of the responses from the server.

1xx (Informational Responses)

These are used for notification or status update purposes. They simply mean that the request that the client generated is being processed but has not been completed yet.

  1. 100 → Continue

    Analogy → You went to a restaurant and gave an order to the waiter of 2 pizzas. The waiter said “Ok, carry on and keep on giving your order.” You then gave an order of 5 cold drinks, 4 burgers & 2 coffees.

    The waiter in the above analogy is your server. It returns 100 status code when it has to tell the client that it has received the request headers & now it can send the request body as well.

  2. 101 → Switching Protocols

    Analogy → You gave an order of 5 Burgers to the waiter but later on said “No, no kindly change my order to 5 Pizzas.

    This status code is generally used when the connection is being upgraded. For example → Switching to WebSocket.

2xx (Success Responses)

These codes are used to tell the client that everything is going well and good.

  1. 200 → OK

    Analogy You went to the restaurant and asked the waiter “Do you have cheese burgers?” The waiter responded “Yes we have plenty of them”

    This status code is used when the request is successful and the server is sending the requested data.

  2. 201 → Created

    Analogy You went to the restaurant and told the waiter “You should add Creamy Burger to your Menu, it is delicious” The waiter immediately added Creamy Burger to the Menu.

    This status code is used when some new resource gets created according to the request generated.

  3. 202 → Accepted

    Analogy You went to the restaurant and told the waiter “I don’t want those old burgers! I want customised burgers with no buns!” The waiter told you “Ok, we are making them for you but they are not ready yet”

    This status code is used when server performs asynchronous requests in the background.

  4. 204 → No Content to be returned in response body

    Analogy You went to the restaurant and told the waiter “You guys cook really good food!” The waiter said nothing in return and just smiled.

    This status code is used when the request is successful but there is no content/data to be returned in the response body by the server. For example → A delete request.

3xx (Redirection Responses)

These codes are used to tell the client that it needs to do some extra steps in order to get the request accepted.

  1. 301 → Moved permanently

    Analogy You went to your favourite restaurant which you were going to in this whole article till now, but found out that they have permanently shifted to some other city!

    This status code is used when the resource request has moved to some other location.

  2. 302 → Found but at a temporary location

    Analogy You went to your favourite restaurant and saw that they have temporarily shifted their dining area to the shop in front of the restaurant because of construction purposes.

    This status code is used when there are maintenance going on over the resource requested and client gets temporarily redirected to some other resource. This is commonly seen when webpages go under maintenance.

  3. 303 → See other as well

    Analogy You went to your favourite restaurant and gave the order at the order area and the worker told you to go to the next counter as well to pay for the order.

    This status code is used generally when you sign up at a website and then you get redirected to another web page to verify your email-id. Basically used when server wants you to redirect to some other page after form submission (after POST requests)

  4. 304 → Not modified since last time

    Analogy You went to the restaurant and told the waiter “Give me 5 burgers” The waiter replied “You already have 5 burgers, there is no need to order more!”

    This status code is used when the client already has the cached version of the same resource that it is requesting.

  5. 307 → Temporary redirect

    Same as 302? → Not exactly

    Analogy for 302 You went to the restaurant by a car and found the restaurant dining area has been shifted 5 Kilometres ahead of where it was initially. You can cover the next 5 KM either by the same car you came in or with any other vehicle of your choice be it helicopter, cycle or any other vehicle. You may change the vehicle or may not change.

    Analogy for 307 You went to the restaurant by a car and found the restaurant dining area has been shifted 5 Kilometres ahead of where it was initially. This time the restaurant owner ordered you that you will have to come in the same car that you came in initially & he will not accept you in any other vehicle.

    Above analogies are enough to understand the difference between 302 & 307.

    This status code is used when server wants to redirect but also want to preserve the request method (GET, POST, DELETE etc.)

4xx (Client Error Responses)

These status codes are used to tell the client that there is some problem/error in the request that the client has generated.

  1. 400 → Bad request

    Analogy You told the waiter “I want dim-dom-dim-dom” The waiter responded “I can’t understand your order, what do you mean by dim-dom-dim-dom*?!”*

    This status code is used when there is a malformed request generated by the client. For example, incorrect URL format.

  2. 401 → Unauthorised request

    Analogy You told the waiter “I want to enter your cash vault” The waiter responded “Sorry, you are not authorised to enter our vault”

    This status code is used when wrong login credentials are passed.

  3. 403 → Forbidden access

    Analogy You told the waiter “Let me talk to the owner of the restaurant” The waiter responded “You are only allowed to eat here. You are not allowed (forbidden) to talk with our owner”

    This status code is used when login credentials were correct or the request was accepted but a particular section of the website was not allowed to be used by the client.

  4. 404 → Not found

    Analogy You told the waiter “I want water, but it should be solid and not liquid” The waiter replied “Sorry, this type of water does not exist and can’t be found”

    This status code is used when client requests a resource that does not exist or has been removed.

  5. 405 → Wrong method

    Analogy You went to the restaurant by car. The restaurant only allows customers who come by an airplane!

    This status code is used when wrong METHOD(GET, POST, DELETE etc.) is used by the client to request the resource. For example, used GET in place of POST.

  6. 408 → Request Timeout

    Analogy You went to the restaurant and waiter came to you. You remained silent and did not give any order. The whole day passed by, the restaurant got closed but you were not able to give a single order & stayed silent for the whole day!

    This status code is used when client takes too much time to send data or if there are some network issues.

  7. 429 → Too many requests

    Analogy You kept on giving the orders to waiter. After some time, the waiter said, “Just stop it! I can’t handle this many orders at a single time!”

    This status code is used to limit the number of requests the client can generate in a time frame. For example, a 60 second window → Rate Limiting

5xx (Server Error Responses)

These status codes are used when the server fails to fulfil an appropriate & valid request because of it’s own fault.

  1. 500 → Internal server error

    Analogy You gave on order of 5 burgers to the waiter but because of the chef’s bad cooking your burgers got overcooked and burnt.

    This status code is used when some unexpected error happens on the server side. For example, a bug in the server code or something else.

  2. 501 → Not implemented

    Analogy You gave an order of 5 chocolate cakes to the waiter but the waiter said “We don’t take cake orders sir. These type of orders are not supported here.”

    This status code is used when the server does not recognise the functionality or the request METHOD (GET, POST, DELETE etc.) used by the client.

  3. 502 → Bad gateway

    Analogy You gave an order to the waiter for 5 burgers. The waiter went to the chef and told him to make 5 burgers. The chef replied “I can’t do this, I am feeling sleepy!”

    This status code is used when one server acting as the gateway (think of it as intermediate server) receives an invalid response from an upstream server (think of it as final server on which the intermediate server is dependent).

    But why BAD GATEWAY? Why not BAD UPSTREAM SERVER?

    Answer → It is because the communication was to happen through this gateway only. This gateway proved to be bad for communication.

  4. 503 → Service unavailable

    Analogy You gave an order to the waiter. The waiter replied “Sorry, we are temporarily closed today because of maintenance work”

    This status code is used when the server is not able to process a request because of overloading or because of some temporary maintenance going on.

  5. 504 → Gateway timeout

    Analogy You gave an order to the waiter. The waiter passed on the order to the chef but the chef took too much time and was not able to give burgers to the waiter. Therefore, you were not able to get the burgers.

    This status code is used when the upstream server takes too much time to respond to the gateway server.


Conclusion

Knowing the HTTP Status codes become important to check the server responses when building something or working with APIs. This article is enough to understand what each Status Code means and can act as a one stop solution if you have any difficulties in understanding these codes. I hope that this article proved useful to you and I was able to add value to your learning process. Follow for more such articles.