01 restful specification
1. What is restful specification
restful is a set of rules, is a convention for data transfer between applications
- REST has nothing to do with technology, represent a style of software architecture, REST is Representational State Transfer short, the Chinese translation is '' to characterize the state transition ''
- REST in terms of resources to look at the entire network, it identifies the distribution of resources in the network, a node by its URL, the client application to obtain the resources characterized by URL, access to these applications characterize the status of these transitions
- REST has nothing to do with technology, represent a style of software architecture, REST is Representational State Transfer short, the Chinese translation is '' to characterize the state transition ''
- So the data, but is acquired through a network or operating (additions and deletions to change search) data, all resources, all data will be treated as REST resources are the most essential attribute different from other architectural styles
For this resource-oriented REST architectural style, it was suggested that a new structure concept, namely: resource-oriented architecture (ROA: Resource Oriented Architecture)
2.restful specification details
Recommended use https instead of http
- To ensure data security
Add api logo in the url
This url to see that this is a api, recommended that the domain name on the back, so there will be no cross-domain problems
The following recommended wording, because he would not cross-domain problems
http://www.lbzhk.com/api/....
Of course, you can also write
http://api.lbzhk.com/....
It reflects the version information in the interface
Convenient to version iterations,
Example:
http://www.lbzhk.com/api/v1....
Note: The version can also be placed in the request header
http://www.lbzhk.com/api/ accept: ...
General resources are used nouns
Everything is restful, also known as resource-oriented programming resources, depending on the Internet
Example:
http://www.lbzhk.com/api/user/
If you want to add some filtering criteria, can be written in the url
Example:
http://www.lbzhk.com/api/user/?page=1&type=9
According to a different method to do different operations
- get, get data
- post, add
- put, update
- patch, partial update
- delete, delete
- options, pre-screening
Status code returned to the user
- 200, success
- 300,301 permanent redirection / 302 temporary redirection
- 400, 403 Access Denied / 404 Not Found
- 500, server code errors
Return different data from different requests
Example:
GET http://www.lbzhk.com/api/user/ [ {'id':1,'name':'张三','age':19}, {'id':1,'name':'李四','age':19}, ] POST http://www.lbzhk.com/api/user/ {'id':1,'name':'张三','age':19} GET http://www.lbzhk.com/api/user/2/ {'id':2,'name':'张三','age':19} PUT http://www.lbzhk.com/api/user/2/ {'id':2,'name':'张三','age':19} PATCH https//www.lbzhk.com/api/user/2/ {'id':2,'name':'张三','age':19} DELETE https//www.luffycity.com/api/user/2/ 空
If there are abnormal returns an error message
Example:
{ error: "Invalid API key" }
For the next request to return an interface
Example:
{ 'id':2, 'name':'alex', 'age':19, 'depart': "http://www.luffycity.com/api/user/30/" }