The Heroku team summarizes some of the HTTP API design guidelines based on Heroku platform APIs and their own in-house system practices, which are posted on github.
Address: Https://github.com/interagent/http-api-design
In view of too many companies to see the HTTP interface does not follow the standard specification to implement, recommended to do the service interface design and front-end students look.
Some of them are very important to draw out:
- Return the correct status code
Do not always use 200! 201,202 It is very meaningful to correspond to different method. Error should return the correct status code, is the request parameter error please return 400, is the server internal error please return 500, the server pressure is too large to kill connection please return 503, for the behavior error instead of the permission problem do not use 401 and 403. These status codes themselves make sense, and do not need to create code or use extra attributes.
- Use UTC time in ISO8601 format
Remember to use UTC and format uniformly, and really need to refer to Moment.js in each project to process the time? Time zone and format enough headache, moment.js is not omnipotent.
- Give a structured error message
Error do not return to the state only, can give a certain error message in the case to return a unified structured error message in the body. This allows the client to make the correct processing, whether to wait for a retry or to stop the request from notifying the user.
Of course, there are a lot of them.