当前位置:网站首页>Restful interface specification
Restful interface specification
2022-07-26 22:42:00 【Everything is lovely 33】
1.Restful
REST The full name is Representational State Transfer, Chinese means to express ( Editor's note : Representational state transition )
RESTful It's a definition Web API Interface design style , It is especially suitable for the application mode of front and rear end separation . The idea of this style is that the back-end development task is to provide data , What we provide externally is the access interface of data resources , So when defining interfaces , Client access URL The path represents the data resource to be operated . in fact , We can use any framework to achieve compliance with restful canonical API Interface . RESTful As the most popular API design code , It must have its unique charm : Powerful 、 brief introduction 、 Easy to use
2.Restful Ten interface specifications of
# 10 Rules
1. Data security :
url Links are generally used https Protocol for transmission notes : use https agreement , It can improve the security in the process of data interaction2. Interface feature representation , You know it's a api Interface
- use api Keyword identification interface url:
- [https://api.baidu.com](https://api.baidu.com/)
- https://www.baidu.com/api
notes : notice api The word , On behalf of the request url The link is to complete the data interaction between the front and back stations
- Luffy's interface :https://api.luffycity.com/api/v1/course/free/3. Multiple data versions coexist
- stay url The link identifies the data version
- https://api.baidu.com/v1
- https://api.baidu.com/v2
notes :url In the link v1、v2 It's the embodiment of different data versions ( Only when a data resource has multiple versions : For example, add interfaces to the original version , Similar revision , You also need to ensure that the old version can be used )4. Data is a resource ( They all use nouns )
The interface is generally used to complete the data interaction between the front and the back , Interactive data is called resource
https://api.baidu.com/users
https://api.baidu.com/books
https://api.baidu.com/book
notes : It is generally advocated to use the plural form of resources , stay url Do not use verbs to operate resources in links , Error model :https://api.baidu.com/delete-user
A special interface can have Verbs , Because these interfaces generally do not have a clear resource , Or the verb is the core meaning of the interface
https://api.baidu.com/place/search
https://api.baidu.com/login5. Resource operation is determined by the request mode (method)
- Operating resources usually involves adding, deleting, modifying and querying , We provide a request method to identify the addition, deletion, modification and query actions
- https://api.baidu.com/books - get request : Get all books
- https://api.baidu.com/books/1 - get request : Get the primary key as 1 The book of
- https://api.baidu.com/books - post request : Add a new book
- https://api.baidu.com/books/1 - put request : Change the primary key as a whole to 1 The book of
- https://api.baidu.com/books/1 - patch request : Change the primary key to 1 The book of
- https://api.baidu.com/books/1 - delete request : Delete the primary key as 1 The book of6. Filter , By means of url Pass search terms in the form of upload parameters
- https://api.example.com/v1/zoos?limit=10: Specify the number of returned records
- https://api.example.com/v1/zoos?offset=10: Specify where to start the return record
- https://api.example.com/v1/zoos?page=2&per_page=100: Specify page , And the number of records per page
- https://api.example.com/v1/zoos?sortby=name&order=asc: Specifies which attribute to sort the returned results by , And sort order
- https://api.example.com/v1/zoos?animal_type_id=1: Specify filter criteria7. Response status code
7.1 Normal response
- Response status code 2xx
- 200: Regular requests
- 201: Create success
7.2 Redirect response
- Response status code 3xx
- 301: Permanent redirection
- 302: Redirect temporarily
7.3 Client exception
- Response status code 4xx
- 403: Request no permission
- 404: The request path does not exist
- 405: The request method does not exist
7.4 Server exception
- Response status code 5xx
- 500: Server exception8. Error handling , Error message expected ,error treat as key
{
error: " No permission to operate "
}9. Return results , For different operations , The results returned by the server to the user should conform to the following specifications
GET /collection: Returns a list of resource objects ( Array )
GET /collection/resource: Return a single resource object
POST /collection: Return the newly generated resource object
PUT /collection/resource: Returns the complete resource object
PATCH /collection/resource: Returns the complete resource object
DELETE /collection/resource: Return an empty document10. need url Requested resources : Request links that require access to resources
# Hypermedia API,RESTful API Better do Hypermedia, That is, the link is provided in the returned result , Connect to others API Method , Make users not look up documents , And know what to do next
{
"status": 0,
"msg": "ok",
"results":[
{
"name":" KFC ( Luo Restaurant )",
"img": "https://image.baidu.com/kfc/001.png"
}
...
]
}
边栏推荐
- Page file system based on C language
- Embedded SIG | 分布式软总线
- y78.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(九)
- Sort out each order when you are in love (it takes two months to sort out in detail)
- leetcode:857. 雇佣 K 名工人的最低成本【分块思考 + 由简单入手】
- CSRC: agree to the IPO registration of Chuanyin holding science and Technology Innovation Board
- 《强化学习周刊》第55期:LB-SGD、MSP-DRL & 对抗鲁棒强化学习
- 商汤科技CEO徐立:公司估值已超70亿美元,不急于上市
- After closing the Suzhou plant, Omron Dongguan plant announced its dissolution, and more than 2000 people are facing unemployment!
- 功耗降低、功能升级!启英泰伦发布二代语音AI芯片:模组价格低至14.99元!
猜你喜欢

Write golang simple C2 remote control based on grpc

动态规划之线性DP

测试开发是开发吗?

Fujian vies for vc/pe

The PRI that Hillhouse joined, including Junlian, Huakong, Shengshi, Lvdong and other 100 institutions, was killed

博途PLC输出通道映射FC

Nacos作为注册中心、配置中心入门使用篇-实现远程调用、动态获取配置文件、数据库配置信息

Docker uses mysql:5.6 and owncloud image to build a personal network disk, install and build a private warehouse harbor

MySQL recommendation

Apifox--比 Postman 还好用的 API 测试工具
随机推荐
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ )
Network and VPC hands-on experiment
毕业5年,从信息管理转行软件测试工程师,我的月薪终于突破了12k
Unity operates on Explorer, opens explorer, selects files, and filters files
Let the program do one thing in one or more seconds
思立微的反击:汇顶涉案屏下光学指纹专利被宣告无效
Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China
恋爱时各个顺序整理(历时两个月详细整理)
博途PLC输出通道映射FC
nvidia-smi报错:NVIDIA-SMI has failed because it couldn‘t communicate with the NVIDIA driver 完整记录
Embedded sig | distributed soft bus
【HCIP】OSPF 路由计算
LeetCode 121:买卖股票的最佳时机
After changing the password in MySQL under win10, the problem of sudden login failure is solved
Inventory of financial institutions
Fujian vies for vc/pe
Sell 700million a year, and get ready for the IPO of Dezhou braised chicken
A13处理器成iPhone11系列最大亮点:性能是麒麟980两倍!
挖财钱堂和启牛学堂哪个更好一些?是安全的吗
Chapter 15 MySQL user management