当前位置:网站首页>Restful API design specification
Restful API design specification
2022-07-06 08:29:00 【Liu Chu, Ge Nian】
PDF Download address :https://download.csdn.net/download/weixin_45525272/80429647
List of articles
- RESTful API design code
- ⼀、 What is? RESTful?
- Two 、 Design concepts and guidelines
- 3、 ... and 、 Why use RESTful API?
- Four 、 agreement
- 5、 ... and 、 domain name
- 6、 ... and 、 edition
- 7、 ... and 、http request ⽅ type
- 8、 ... and 、 route ( route )
- Nine 、 Filtering information (url in ? after ⾯ Parameters of )
- Ten 、 Status code
- 11、 ... and 、 Other
RESTful API design code
⼀、 What is? RESTful?
⼜ call REST(Representational State Transfer),⼀ Kind of Software architecture ⻛ grid 、 Design style , Not the standard , It just provides ⼀ Set design principles and constraints . It mainly ⽤ Software for client and server interaction .
Its core is ⾯ To resources ,REST Specifically for the network should ⽤ Design and development methods , To reduce the complexity of development , carry ⾼ Scalability of the system .
stay RESTful Architecture , Each website represents ⼀ Kind of resources (resource), So there can't be verbs in the website , Only nouns .
⼀ In general , The tables in the database are the same kind of records ” aggregate ”(collection), therefore API Nouns in should also make ⽤ The plural , Unless there is no proper plural form , Such as :weather.
Two 、 Design concepts and guidelines
- Everything on the network can be abstracted as resources (resource);
- Every resource has only ⼀ Resource ID for (resource identifier), Operations on resources do not change these identities ;
- All operations are stateless ;
- Layered system , Representation component ⽆ Can't understand the components outside the middle tier with which it interacts . By limiting system knowledge to a single layer , Can limit the complexity of the entire system , Promote the uniqueness of the bottom ⽴ sex .
3、 ... and 、 Why use RESTful API?
- Resources oriented (URI), Interpretive ;
- Behavior (GET / POST / PUT / PATCH / DELETE) And resources (URI) Separate , Lighter weight ;
- Data description is simple , send ⽤JSON、XML、PROTOBUFFER You can fully cover , Mainly make ⽤JSON;
Four 、 agreement
API And ⽤ User's communication protocol ,⼀ General envoy ⽤HTTP agreement , Make ⽤HTTPS.
5、 ... and 、 domain name
We should try our best to API Deployed in specialized ⽤ Under domain name :
https://api.example.com
If determined API It's simple , There will be no progress ⼀ Step expansion , Consider putting it under the primary domain name :
https://example.org/api/
6、 ... and 、 edition
At every API Corresponding URL in , due ⼀ Version number , In order to upgrade the service in the future , All versions of the client can be used normally , as follows :
https://api.example.com/v1/topics/
https://api.example.com/v2/topics/
https://api.example.com/v3/topics/
7、 ... and 、http request ⽅ type
Request mode | meaning |
---|---|
GET | Read (Read) |
POST | newly build (Create) |
PUT | to update (Update), Usually all updates |
PATCH | to update (Update), It's usually a partial update |
DELETE | Delete (Delete) |
8、 ... and 、 route ( route )
• Every ⽹ There must be no verbs in the address , Only nouns . And should make ⽤ The plural , except ⾮ There is no proper plural , Such as :weather.
https://api.example.com/v1/topics/ —> All posts ⼦
• For resources under individual or class names , You can directly add specific id To represent , as follows :
https://api.example.com/v1/topics/100001/info // id by 100001 My post ⼦ Details of
https://api.example.com/v1/users/12345/topics/ // id by 12345 Of ⽤ All posts under the account name ⼦
• More examples [ With request method ]
[GET] https://api.example.com/v1/topics/ — Get all posts ⼦( list )
[POST] https://api.example.com/v1/topics/ — New post ⼦
[PUT] https://api.example.com/v1/topics/100001 — Update the full post ⼦
[PATCH] https://api.example.com/v1/topics/100001 — Update post ⼦ Some information
[DELETE] https://api.example.com/v1/topics/100001 — Delete post ⼦
[GET] https://api.example.com/v1/groups/1/topics/ — Get all posts in a group ⼦( list )
[GET] https://api.example.com/v1/users/12345/profile — Get some ⽤ Household information
[PUT] https://api.example.com/v1/users/12345/profile — Update sth ⽤ Household information
[GET] https://api.example.com/v1/users/12345/labels — Get some ⽤ All labels
Nine 、 Filtering information (url in ? after ⾯ Parameters of )
Reasons for using filtered information :
single ⼀ Of url The path cannot represent all the scenes , Played a supplementary role ⽤;
?limit=10 : Specify the number of returned records ?offset=10 : Specify where to start the return record . ?page=2&per_page=100 : Designate the ⼏⻚, And every ⻚ Number of records . ?sortby=name&order=asc : Specifies which attribute to sort the returned results by , And sort order . ?animal_type_id=1 : Specify filter criteria
Avoid multiple levels URL, Not conducive to expansion , The meaning is not clear , It's difficult to understand ;
multistage URL:GET /authors/12/categories/2 // What does it mean ?
The right thing to do is :
GET /authors/12?categories=2
Ten 、 Status code
HTTP The status code is ⼀ Three digits , Divided into five categories :
Category | meaning |
---|---|
1xx | Related information , Don't use |
2xx | Successful operation |
3xx | Redirect |
4xx | Client error |
5xx | Server error |
Unless it is 500 and 404 error ,⼤ Partially make ⽤ 200 Status code is enough , The returned data format is as follows :
11、 ... and 、 Other
(1)API Of ⾝ Certificates should use OAuth 2.0 frame .
(2) Data format returned by the server , Should be used as much as possible JSON, Avoid using XML.
边栏推荐
- Circular reference of ES6 module
- FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
- ESP series pin description diagram summary
- sys. argv
- 化不掉的钟薛高,逃不出网红产品的生命周期
- Migrate data from SQL files to tidb
- Vocabulary notes for postgraduate entrance examination (3)
- LDAP应用篇(4)Jenkins接入
- What is CSRF (Cross Site Request Forgery)?
- Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
猜你喜欢
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
Online yaml to CSV tool
Online yaml to CSV tool
2022.02.13 - NC004. Print number of loops
Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
根据csv文件某一列字符串中某个数字排序
Ruffian Heng embedded bimonthly, issue 49
[MySQL] database stored procedure and storage function clearance tutorial (full version)
matplotlib. Widgets are easy to use
随机推荐
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
【Nvidia开发板】常见问题集 (不定时更新)
Analysis of pointer and array written test questions
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
化不掉的钟薛高,逃不出网红产品的生命周期
Résumé des diagrammes de description des broches de la série ESP
按位逻辑运算符
Yyds dry goods inventory three JS source code interpretation eventdispatcher
China polyether amine Market Forecast and investment strategy report (2022 Edition)
C language custom type: struct
CISP-PTE实操练习讲解
Introduction to number theory (greatest common divisor, prime sieve, inverse element)
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
sublime text的编写程序时的Tab和空格缩进问题
延迟初始化和密封类
egg. JS directory structure
Research and investment forecast report of citronellol industry in China (2022 Edition)
Vocabulary notes for postgraduate entrance examination (3)