当前位置:网站首页>Restful接口规范
Restful接口规范
2022-07-26 21:56:00 【万物皆可爱33】
1.Restful
REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征性状态转移)
RESTful是一种定义Web API接口的设计风格,尤其适用于前后端分离的应用模式中。 这种风格的理念认为后端开发任务就是提供数据的,对外提供的是数据资源的访问接口,所以在定义接口时,客户端访问的URL路径就表示这种要操作的数据资源。 事实上,我们可以使用任何一个框架都可以实现符合restful规范的API接口. RESTful作为目前最流行的 API 设计规范,一定有着它独有的魅力:强大、简介、易上手
2.Restful的十条接口规范
# 10条规范
1.数据的安全保障:
url链接一般都采用https协议进行传输 注:采用https协议,可以提高数据交互过程中的安全性2.接口特征表现,一看就知道是个api接口
- 用api关键字标识接口url:
- [https://api.baidu.com](https://api.baidu.com/)
- https://www.baidu.com/api
注:看到api字眼,就代表该请求url链接是完成前后台数据交互的
-路飞的接口:https://api.luffycity.com/api/v1/course/free/3.多数据版本共存
- 在url链接中标识数据版本
- https://api.baidu.com/v1
- https://api.baidu.com/v2
注:url链接中的v1、v2就是不同数据版本的体现(只有在一种数据资源有多版本情况下:例如在原有版本上增加接口,类似改版,也需要保证老版本可以使用)4.数据即资源(均使用名词)
接口一般都是完成前后台数据的交互,交互的数据我们称之为资源
https://api.baidu.com/users
https://api.baidu.com/books
https://api.baidu.com/book
注:一般提倡用资源的复数形式,在url链接中奖励不要出现操作资源的动词,错误示范:https://api.baidu.com/delete-user
特殊的接口可以出现动词,因为这些接口一般没有一个明确的资源,或是动词就是接口的核心含义
https://api.baidu.com/place/search
https://api.baidu.com/login5.资源操作由请求方式决定(method)
- 操作资源一般都会涉及到增删改查,我们提供请求方式来标识增删改查动作
- https://api.baidu.com/books - get请求:获取所有书
- https://api.baidu.com/books/1 - get请求:获取主键为1的书
- https://api.baidu.com/books - post请求:新增一本书书
- https://api.baidu.com/books/1 - put请求:整体修改主键为1的书
- https://api.baidu.com/books/1 - patch请求:局部修改主键为1的书
- https://api.baidu.com/books/1 - delete请求:删除主键为1的书6.过滤,通过在url上传参的形式传递搜索条件
- https://api.example.com/v1/zoos?limit=10:指定返回记录的数量
- https://api.example.com/v1/zoos?offset=10:指定返回记录的开始位置
- https://api.example.com/v1/zoos?page=2&per_page=100:指定第几页,以及每页的记录数
- https://api.example.com/v1/zoos?sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序
- https://api.example.com/v1/zoos?animal_type_id=1:指定筛选条件7.响应状态码
7.1 正常响应
- 响应状态码2xx
- 200:常规请求
- 201:创建成功
7.2 重定向响应
- 响应状态码3xx
- 301:永久重定向
- 302:暂时重定向
7.3 客户端异常
- 响应状态码4xx
- 403:请求无权限
- 404:请求路径不存在
- 405:请求方法不存在
7.4 服务器异常
- 响应状态码5xx
- 500:服务器异常8.错误处理,应返回错误信息,error当做key
{
error: "无权限操作"
}9.返回结果,针对不同操作,服务器向用户返回的结果应该符合以下规范
GET /collection:返回资源对象的列表(数组)
GET /collection/resource:返回单个资源对象
POST /collection:返回新生成的资源对象
PUT /collection/resource:返回完整的资源对象
PATCH /collection/resource:返回完整的资源对象
DELETE /collection/resource:返回一个空文档10.需要url请求的资源:需要访问资源的请求链接
# Hypermedia API,RESTful API最好做到Hypermedia,即返回结果中提供链接,连向其他API方法,使得用户不查文档,也知道下一步应该做什么
{
"status": 0,
"msg": "ok",
"results":[
{
"name":"肯德基(罗餐厅)",
"img": "https://image.baidu.com/kfc/001.png"
}
...
]
}
边栏推荐
- Instructions for use of light source controller dial switch
- Summary of debugging stc8a8k64d4 MCU 485 communication
- 华为Atlas900揭秘:集成数千颗昇腾910芯片,算力堪比50万台PC!
- Embedded sig | distributed soft bus
- Internet celebrity spicy bars can't catch young people
- Detailed explanation of SQL secondary injection
- nvidia-smi报错:NVIDIA-SMI has failed because it couldn‘t communicate with the NVIDIA driver 完整记录
- [IO Development Notes] smart cloud intelligent watering device practice (3) - automatic code generation and transplantation
- Summary of shell related commands
- LeetCode 122:买卖股票的最佳时机 II
猜你喜欢

2018 arXiv preprint | MolGAN: An implicit generative model for small molecular graphs

Overview of banking industry

Chapter 15 MySQL user management

Makefile related syntax summary (openc910)

Five years after graduation, I changed from information management to software testing engineer, and my monthly salary finally exceeded 12K

Those environment configurations and plug-ins of idea

7.27 watch first | openeuler is ambitious, open source Huizhi creates the future - the most detailed agenda of the Euler sub forum of the open atom global open source summit is released

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

【Io开发笔记】机智云智能浇花器实战(1)-基础Demo实现

Embedded SIG | 分布式软总线
随机推荐
推动ARM服务器芯片替代X86,华为、飞腾扛起国产化大旗!
顺序表实现
Unity operates on Explorer, opens explorer, selects files, and filters files
MySQL summarizes common scenarios of MySQL index failure
JSON字符串转化为JSON对象,获取某个key的值,判断某个key是否存在
【Io开发笔记】机智云智能浇花器实战(3)-自动生成代码移植
麒麟990系列为何无缘Cortex-A77和Mali G77?
Detailed explanation of SQL secondary injection
Overview of banking industry
2018 arXiv preprint | MolGAN: An implicit generative model for small molecular graphs
Summary of debugging stc8a8k64d4 MCU 485 communication
v-model语法糖的实现
Chapter 15 MySQL user management
Excel VBA quick start (XII. Common usage of like comparison)
IDEA的那些环境配置及插件
缓存数据库Memcached
View drawing process 1-the relationship between view and window
Embedded SIG | 分布式软总线
Regular expressions and bypass cases
Blog Garden beautification skills summary