当前位置:网站首页>Restful API 接口规范
Restful API 接口规范
2022-06-12 20:18:00 【谬也】
1、路径
- 在RESTful架构中,每个网址代表一种资源(resource),所以网址中不能有动词,只能有名词,而且所用的名词往往与数据库的表格名对应。且一般为复数。
- "/“只是⼀个分隔符,”/“不应该出现在URL的末尾。如果访问到末尾含有”/“的地址,应该重定向到没有”/"的地址。
- /{version}/{resources}/{resource_id}/{sub_resources}/{sub_resource_id}/{sub_resource_property}
2、方法
对于资源的具体操作类型,由HTTP动词表示,HTTP动词主要有以下几种,括号中对应的是SQL命令。
GET(SELECT):从服务器取出资源(一项或多项);POST(CREATE):在服务器新建一个资源;PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源);PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性);DELET(DELETE):从服务器删除资源;HEAD:获取资源的元数据;OPTIONS:获取信息,关于资源的哪些属性是客户端可以改变的。
3、过滤信息
如果记录数量很多,服务器不可能都将它们返回给用户,API会提供参数,过滤返回结果,常见的参数有:
?limit=20指定返回记录的数量为20;
?offset=8指定返回记录的开始位置为8;
?page=1&per_page=50指定第1页,以及每页的记录数为50;
?sortby=name&order=asc指定返回结果按照name属性进行升序排序;
?animal_type_id=2指定筛选条件。
4、状态码
使用适合的状态码很重要,而不应该全部都返回状态码 200
200~299段 表示操作成功:
200 操作成功,正常返回
201 操作成功,已经正在处理该请求
300~399段 表示参数方面的异常
300 参数类型错误
301 参数格式错误
302 参数超出正常取值范围
303 token过期
304 token无效
400~499段 表示请求地址方面的异常:
400 找不到地址
403 权限不足
404 找不到资源
500~599段 表示内部代码异常:
500 服务器代码异常
5、返回数据
尽量使用json,避免使用xml
参考链接
https://blog.csdn.net/qq_38423105/article/details/120722022
https://blog.csdn.net/a2398936046/article/details/124764479
如有不对,烦请指出,感谢~
边栏推荐
- 登錄mysql
- Unsupported class file major version 60
- 牛客網:三數之和
- Torch network model is converted to onnx format and visualized
- What does MySQL full value match mean
- Wall Street cheat sheet
- MySQL - the execution order of an SQL statement
- unable to recognize “*.yaml“: no matches for kind “ClusterRole“ in version “rbac.authoriz
- 进程会计、进程时间、守护进程
- How to make a computer installation and startup USB flash disk
猜你喜欢

If you master these skills, you can also write a high score resume in the eyes of HR
![[games101] class note 8 - shading (shading frequency, graphics pipeline, texture mapping)](/img/90/f23c3fd9386521ec9254943bff2a0d.png)
[games101] class note 8 - shading (shading frequency, graphics pipeline, texture mapping)

Niuke net: somme des trois nombres

进程会计、进程时间、守护进程

QT pro文件配置ffmpeg宏

Process creation fork (), demise wait()

EditText控制从左上角开始

华尔街备忘单(Wall Street Cheat Sheet)

exec函数、shell的实现

House raiding 3
随机推荐
Interpreter Files
进程会计、进程时间、守护进程
Since using low code development, the development efficiency has been increased by 10 times
Macro definitions and functions
Analysis of test questions in Chapter 13 of PMP preparation
Alipay payment Episode 12: Crazy God and Feige Alipay payment configuration code (free resources, no thanks for taking them away)
在 Traefik Proxy 2.5 中使用/开发私有插件(Traefik 官方博客)
Demand and business model innovation-5-process
用户权限和组权限
【splishsplash】自定义导出器
[leetcode 7 solution] integer inversion
Experience Technology Department of ant group launched the 2023rd school recruitment
20 shortcut keys for vs code!
What is a hash index?
Microsoft Word 教程,如何在 Word 中插入页眉或页脚?
EditText控制从左上角开始
Index optimization principle
Wechat applet notes
Scalars, vectors, arrays, and matrices
Demand and business model innovation - demand 2- demand basis