As we all know, every HTTP response will be provided with a status code, but for many developers, most usually use several status code is nothing less than 200,400,404,500 and so on. That many other applications in which the state code scenario, when to use what has become a status code worthy of our deep thinking problem. Even in companies like Facebook, those clever API developers may be constructed only 200 returned. To this end, Michael Kropat dedicated to the author analyzes various application scenarios status code, and why do we so carefully distinguish different status code, and also talked about the benefits of doing so.
What is even simpler than that returns an HTTP status code things? Page rendering it? If you render, then return to 200 pictures. Page does not exist? That is 404. We need to redirect the user to another page? Then use 302, maybe 301 lines.
Everything is so simple, but if someone told you, you do not have to REST way to do things, you may have to wake up. Are new resources returned to the RFC-compliant, Roy-Fielding Recommendation status code? It is only 200? Maybe 204 No Content, 202 Accepted, or is it 201 Created?
The problem is that the official HTTP / 1.1 Guide (RFC) was originally released in 1997. At the time we are still using Netscape Navigator, 33.6kbps modem surfing the debugger it. This is like using the same Sun Tzu in the modern business strategy. These valuable suggestions and will not end with the passage of time and change. However, we need to really understand them.
If you have a visual tree is like, it can help you quickly identify with your situation coincides with a state code, so you can not ignore those related to the.
We found that many people will fall into one, and put forward such as "this situation should still use 503 Service Unavailable 404 Not Found it"? stop. If you think about a specific status code in a completely different response categories, it shows that your approach is completely wrong.
I make a few points before continuing:
You do not have to listen to me, please check directly with the RFC 7231 httpstatuses.com
I is intended for developers who create sites or use the REST API
I will respond to code broadly divided into three categories
Finally, mention one thing: I did not actually qualified to express their views on this subject, I just read some of the RFC and the development of some APIs only. If you think I am wrong, or you do not use state codes tend to use, then please comment at the end the middle finger out, we can discuss.
Why is it important status codes
Although Facebook in a lot of smart developers when building APIs return only 200, but I want to say is, the status code is indeed very important. Existing state code for modern websites / API is a little too broad. If the response you want to apply a specific format to include some details, such as which field validation fails, the reasons for that, which would allow clients to a more meaningful way to handle the response. That being the case, why not many take the time to look at those "less common" HTTP status code it?
Turning Why is the use of specific status code is very important when one of the reasons people love to mention is HTTP is a hierarchical system, there may be a proxy between the client and server, cache, or other HTTP libraries if meaningful response code, it will make it all better workplace. However, I think this explanation is untenable, for example, the future we all use on a HTTPS, we have lost all agents and disabled caching node, then you can say what status code is useless?
Here, I think I want to talk about the status code is still a very important three reasons:
1. The client may behave differently (or can easily be extended to deal with) for different status code:
301 Moved Permanently 302 Found for Google and other search engines as well as SEO metaphor
301 Moved Permanently cached meaning, while 429 Too Many Requests meaning no cache, and so on
Client libraries can handle 429 Too Many Requests by a time delay after the retry request
The client library may take a similar approach 503 Service Unavilable
2. Many state codes indicated may be processed through a special response.
Returns 405 Method Not Allowed 404 instead of APIs makes me crazy sometimes, "I was mistyped URL or HTTP using the wrong way to do that"?
Correctly distinguish 502 Bad Gateway 500 Internal Server Error and let you save a lot of debugging time.
3. Regardless Believe it or not, there are many popular APIs to establish a number of conventions, for example, returned 201 Created, 429 Too Many Requests, or 503 Service Unavilable. If you follow these conventions, then the user will be easier to use in your site / API, the encountered problems easier to solve.
If you encounter problems when using the HTTP status code, you can also refer to the following resources:
HTTP status code reference
HTTP status codes used by world-famous APIs
HTTP status codes visualized as a subway map
Status Codes To Cat Memes As a Service
Status Codes To Dog Memes As a Service