When it comes to writing APIs for a web -based service, we will definitely need to use a HTTATS Code depending on the different conditions so that other dowers that use our API are less confused as possible. In this regard, there are some Best Practice that we can make sure that our status codes will be fully understood.
In general, the status codes in the HTTP protocol are divided into the following five groups, but each of the codes themselves contains many subcategories, and to ensure that the other two -blockers interacting with our EPI are well able to understand well. The meaning of the state codes is essential:
- – 1xx
- – 2xx
- – 3xx
- – 4xx
- – 5xx
In general, the importance of the status codes is that they are the first reinsurance point that if the Ricoost sent to the server, it will inform the client (to view a list of status codes, you can refer to the http status codes link. .)
1xx group status codes
HTTP status codes that start with number one contain two general uses. The first use refers to the data -related data data, as well as the 1xx status state codes indicate the primary reacte from the client, such as the 100 (Continue) status code indicating that the server received Request Headers information from the client and waiting for Request Body.
2xx group status codes
The HTTP status codes that start with the number two indicate the success of the ricoist sent to the server. For example, the status code 200 (OK) indicates that the Ricoost has been successfully performed and the Created Status Code also shows that the request has been successfully made and a new reference has been created.
3xx group status codes
The HTTP status codes, which start with the number three, indicate that he must take another action in order to successfully complete the user. For example, the status code of 301 (Moved Permanently) refers to the client to a different URI to fulfill its request.
4xx group status codes
HTTP status codes that start with the number four are also tangible for non -technical users as the most famous status code belongs to this family, 404, is known to many users! In addition, there are other codes in the group that can be used in the API design, including the Too Many Requests Code, which can be used when the number can be used. The ricoists received from a particular client should be over -permitted, which ultimately leads to the rejection of the request by the server.
5xx group status codes
HTTP status codes that start with the number five are all related to the server side problems and have nothing to do with the client at all, and unlike the 4xx family codes, these codes are used when the API is implemented on the server. There is a problem and the server is incapable of applying the client, one of which is the SERVICE Unavailable Status Code, which is used when the service is not available.
What are the fits of a good status code?
A good status code not only informs the client in the process of doing the work, but also shows the client reason or the reasons for the occurrence of a particular event. In general, we can say when we have a good status code to the client that has the following three features:
– contain the status code (eg 200) and also summarize (eg OK).
– contain additional descriptions and more details about the process.
– Contains a conceptual text for users that summarize the code, status, details, relevant reasons, and the suggested solution to the problem.
To better understand how to write a good status code, we will explain the above features:
– Standard Status Codes: Using the standard status codes we have examined above in five different groups can be a good starting point for writing professional status codes. In fact, when you use the family of 4xx or 5xx status codes, the user will quickly realize that there is a problem, whether from the client or on the server side.
– Provide details: If we provide the client with a status code such as Bad Request, although such a code indicates a problem with the user, it does not provide the user with sufficient and practical information so that they can take appropriate action. Consider. In this regard, we need to provide more details to the user because the phrase Bad Request has a general concept and the bad word is completely relative and it is necessary to get it out of relative state and make the concept perfectly illustrated. Let’s make.
– Understanding for users: Although the numerical status codes are well visible for the system (for example, we can check with conditional commands that if the status code was 400, such a job is done), providing understandable details to humans as well. Helping to make the situation more professional.
Now, if we want to examine what is mentioned above, we will find a situation code as you will see below:
{
"code": 429,
"codeMessage": "Too Many Requests",
"errorDetails": "There have been more HTTP requests that you`re allowed to send; To solve this problem, you have to upgrade you plan to Unlimited API Usage."
}
In fact, the above status code contains keys under the titles of CodeMessage, Code, and Errordetails, which contains useful information for the doloper that uses the API that constitutes the three characteristics of a principled situation. In the interpretation of the above status code, we should say that code 429 and the standard Too Many Requests message indicate that the number of requests sent to the server was overly permissible, but what is more than anything for the Doloperi that received this error message, The message is related to the ERRRRORDETIPISS key, which states that “the number of requests sent is too much and you need to upgrade your plan to Unlimited (without restriction) to fix this problem.” That simply provides the solution to Dolopper to fix the problem.
Result
In general, the status codes we use in various projects are completely project -oriented, and no structure that is answered for an API can be used exactly the same way in other projects. In a nutshell, the purpose of providing the status codes in API design is to not only provide information about the type of problem, but also provide a solution to the problem to fix the problem to work with our API experience. They get.