restapi的设计细节和实施是什么样的
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,今天就跟大家聊聊有关restapi的设计细节和实施是什么样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。在RESTful API的设计中,我
千家信息网最后更新 2025年12月02日restapi的设计细节和实施是什么样的
今天就跟大家聊聊有关restapi的设计细节和实施是什么样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
在RESTful API的设计中,我打算自定义一个请求头,把token放进去以便向其他sora组件请求服务。
于是,把之前的代码稍微改成这样:
parser.add_argument('auth-token',type=str,help='put the token here',location='headers')引用该值时,用法如下:
class TodoSimple(Resource): def get(self,todo_id): args = parser.parse_args() if args['auth-token'] == 'thisismytoken': return {todo_id:todos[todo_id]} else: return {'error':'token error'},401 def put(self,todo_id): todos[todo_id] = request.form['data'] return {todo_id:todos[todo_id]}直接
args = parser.parse_args()
然后读取其中的值即可。
另外,之前的测试我只是简单地用-d指定"name=hochikong"操作getname资源,现在把它稍微改下。
class GetName(Resource): def post(self): args = parser.parse_args() name = args['name'] #name = {} #name['ac'] = args['name'] #name = request.json.get('name') return {'yourame':name}但是curl的请求则变成这样:
curl -i -X POST -H 'Content-Type:application/json' -d '{"name":"hochikong"}' http://localhost:5000/getname注意!:我发送的是JSON数据,所以要修改http head为application/json,另外:
'{"name":"hochikong"}'JSON数据中的字符串要用双引号,否则会报错。而JSON数据外还需要套一个引号
我的完整代码:
__author__ = 'hochikong'from flask import Flask,requestfrom flask.ext.restful import Resource,Api,reqparseapp = Flask(__name__)api = Api(app)todos = {'task':'get the list of docker'}parser = reqparse.RequestParser()parser.add_argument('name',type=str,help='get the name') #因为这句话"By default, the RequestParser tries to parse values from flask.Request.values, and flask.Request.json.", #我们不需要在name这个参数后加'location=json',不过加了也无妨parser.add_argument('auth-token',type=str,help='put the token here',location='headers')class TodoSimple(Resource): def get(self,todo_id): args = parser.parse_args() if args['auth-token'] == 'thisismytoken': return {todo_id:todos[todo_id]} else: return {'error':'token error'},401 def put(self,todo_id): todos[todo_id] = request.form['data'] return {todo_id:todos[todo_id]}class GetName(Resource): def post(self): args = parser.parse_args() name = args['name'] #name = {} #name['ac'] = args['name'] #name = request.json.get('name') return {'yourame':name}api.add_resource(TodoSimple,'/')api.add_resource(GetName,'/getname')if __name__ == '__main__': app.run() 启动:
python flaskrr.py
发送请求测试getname资源:
hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$ curl -i -X POST -H 'Content-Type:application/json' -d '{"name":"hochikong"}' http://localhost:5000/getnameHTTP/1.0 200 OKContent-Type: application/jsonContent-Length: 24Server: Werkzeug/0.10.1 Python/2.7.6Date: Sat, 11 Apr 2015 14:07:03 GMT{"yourame": "hochikong"}hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$发送请求测试自定义head:
hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$ curl -X GET -H 'auth-token:thisismytoken' http://localhost:5000/task{"task": "get the list of docker"}hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$如果token不对:
hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$ curl -X GET -H 'auth-token:thisisyourtoken' http://localhost:5000/task{"error": "token error"}hochikong@hochikong-P41T-D3:~/PycharmProjects/untitled/sora_test$测试成功。
不过设计RESTful API最辛苦的还是设计JSON请求格式,各种功能各种格式,我也是醉了
补充:
外部可见的服务器。
运行服务器后,会发现只有你自己的电脑可以使用服务,而网络中的其他电脑却不行。 缺省设置就是这样的,因为在调试模式下该应用的用户可以执行你电脑中的任意 Python 代码。如果你关闭了 调试 或信任你网络中的用户,那么可以让服务器被公开访问。只要像 这样改变 run() 方法的调用:
app.run(host='0.0.0.0')
这行代码告诉你的操作系统监听一个公开的 IP 。
看完上述内容,你们对restapi的设计细节和实施是什么样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
设计
服务
代码
测试
内容
数据
服务器
电脑
细节
引号
格式
用户
网络
资源
不对
不行
成功
操作系统
功能
参数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全属于计算机服务行业吗
管理终端服务器
广东无线软件开发报价表
服务器怎么把键盘调出来
c 循环获得的数据库数据
苹果id账号密码数据库
cnki数据库教程ppt
软件开发项目中的分工
河南国资委华为服务器
路桥区销售软件开发解决方案
海南大学网络安全调剂
mac 数据库服务器推荐
2021网络安全期
网络安全错误711
泉州备件管理软件开发
金蝶 k3服务器安装
新能源气象网络安全
网络安全防范宣传单
应届生网络安全薪资
我的世界方块研究所服务器 要塞的坐标
雷霆之击最奇葩的服务器
软件开发属于数字内容范畴吗
广安网络技术哪家强
服务中断属于网络安全问题吗
计算机网络技术dns
电子软件开发工资高吗
公司app的服务器在哪里
查询到另一数据库
菜小依网络技术工作室
pubg端游连不上服务器