Most Important HTTP Status Codes in 10 Minutes
In this video you will learn all http codes that are necessary while communicating between frontend and backend in web development.
So let's jump right into it.
So here is a link on Wikipedia with all status codes that are possible. It doesn't make any sense to learn them all because only a few are really used in production development.
So why are status important at all? So all web works in a way that we load from backend some data on the client. It doesn't even matter if it's done with javascript like fetch. It happens every single time when you load any page. Status codes helps backend and frontend to communicate better because it's much faster to just give a status code that will fully describe a situation than to define it somehow additionally with information in response for example. This is why status code is the smallest amount of information what we can possibly get from the backend.
Status code is always 3 digits. First one is a class of status code and last 2 digits are specific status code in category.
As you can see our list starts with 100 requests and we can skip them all because we won't use any of them in communicating with API or server.
All requests which are 200 plus are successful requests. Which means each of them at least was received by server and was processed without errors.
The most popular request here is 200. It's a general successful request which just says "Everything is fine" and it may contain some response.
The second popular request is 201. It means created. We actually could use 200 is created case also and actually for all our successful requests and I saw a lot of projects where people were doing this but it's more specific if you use 201 when you created something.
The next popular request is 204. It's a request where client knows for sure that there is no content to get back. It's the only difference from 200 request.
And this when 3 most popular 200 requests. Now let's jump into 300 section.
All 300 requests are about redirects with different reasons. This requests are more used for edge cases and not like basic status codes while building a web application.
301 means that the url moved permanently to new url and it will be forever there.
302 means that the url moved temporary to other url.
304 is a status code about caching. It is possible to set caching headers on the requests and if request was not modified we will use the cached version of it. So 304 means exactly that nothing was changed.
All 400 request generally mean bad request. So it's not like our server is broken but that the request was not correct or not allowed for some reasons.
400 request is a general bad request. So it can be whatever and a lot of people unfortunately use only it to specify that there was an error.
401 is unauthorized request. It's normally used when you are not logged in user and you are trying to get some information what is allowed only for registered used.
403 is forbidden request. It means that you are not allowed to get this data or see this page. This is the good answer if non admin user tries to access admin pages.
404 is the most known method it's a not found. If the resource doesn't exists or was not found this is the only thing that you need to give back.
405 is not that popular and it means that our server doesn't support the method that you tried to use. In my experience it happens very often with options request when backend developers forgot to configure allowed request types properly and option request which is send automatically by browser is not allowed.
418 status code is an "I'm a teapot" and this was actually a April fools joke. It's never used in real projects of course but is quite funny at least when you get this question on interview.
422 is really nice status code but unfortunately people are not using it that often. It means "Unprocessible entities" which is actually that some validation of server didn't allow the request to pass. Normally it means that some fields are not correct are you get a list of validation errors back. I highly recommend to use it if you are giving any sort of validation errors back to the client.
We are almost done because we are coming in 500 section. It's all about server errors. If you see any 500 plus error it means that something really bad happened.
500 is an "Internal server error". It's normally an generic error that server didn't process and now everything is broken.
501 is "Not implemented" status code. It's not popular but actually during building of new API it directly says that something is not implemented yet. So I like to mark all my future endpoints with this answer just at the beginning of implementation.
502 is a "Bad gateway" it generally means that you have some proxy (for example nginx) and you server where you proxy the request is broken or was not started.
503 is a "Service unavailable" and normally is used to indicate that there are too many requests or load is too high this is why server can't do anything with your request.
This were the most popular requests that people are typically using in production. All other requests you can simply ignore because they are almost never used.
Want to conquer your next JavaScript interview? Download my FREE PDF - Pass Your JS Interview with Confidence and start preparing for success today!