当前位置:网站首页>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 criteriaAvoid 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.
边栏推荐
- TiDB备份与恢复简介
- [2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
- 【MySQL】锁
- LDAP Application Section (4) Jenkins Access
- Use Alibaba icon in uniapp
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- 指针进阶---指针数组,数组指针
- Online yaml to CSV tool
- MySQL learning records 12jdbc operation transactions
- Sort according to a number in a string in a column of CSV file
猜你喜欢

JS native implementation shuttle box

JVM performance tuning and practical basic theory - Part 1

2022.02.13 - NC002. sort

堆排序详解

Configuring OSPF load sharing for Huawei devices

Online yaml to CSV tool

PLT in Matplotlib tight_ layout()

根据csv文件某一列字符串中某个数字排序

【MySQL】数据库的存储过程与存储函数通关教程(完整版)

Analysis of pointer and array written test questions
随机推荐
logback1.3. X configuration details and Practice
pytorch训练好的模型在加载和保存过程中的问题
LDAP应用篇(4)Jenkins接入
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
torch建立的网络模型使用torchviz显示
Colorlog combined with logging to print colored logs
Process of obtaining the electronic version of academic qualifications of xuexin.com
[cloud native] teach you how to build ferry open source work order system
Golang force buckle leetcode 1020 Number of enclaves
升级 TiDB Operator
指针进阶---指针数组,数组指针
Grayscale upgrade tidb operator
Unified ordering background interface product description Chinese garbled
Circular reference of ES6 module
leetcode刷题 (5.28) 哈希表
3. File operation 3-with
【MySQL】锁
堆排序详解
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
LDAP application (4) Jenkins access