When you are building a REST API, make sure you follow all the best practices. One of them is using HTTP status codes. Let’s take a look at them.
HTTP status codes are there to help the client understand whether the request was successful or not. They are numbers from 100s to 500s, and each has a separate meaning. These codes are sent to the client along with the response. The client also relies on these codes for implementing different error handling techniques. For instance, the developer can set to display an error message if he receives a 401 status code to let the user know to sign up first.
Let’s look at some of the most common HTTP status codes and their use in an application.
This status code tells you that everything went smoothly, and the request was a success.
It tells you that the request was successful, and also it created a new record. It is generally used with PUT/POST methods.
It tells you that the server can not process the request due to a client-side error. You would get this if you didn’t pass the payload where required or have invalid formatting.
If you try to access a page that requires user authentication, you will receive a 401 status code. You would need to authenticate yourself to open the webpage you want.
You get this status code when you are authenticated but do not have access rights to a particular webpage.
It tells you that the page you are trying to access is not available.
When the server needs to shut down an idle connection, it sends the 408 status code.
It tells that the server does not know how to process the HTTP request.