由于网络问题就不放图了
在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,request from flask.ext.restful import Resource,Api,reqparse app = 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,'/<string:todo_id>') 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/getname HTTP/1.0 200 OK Content-Type: application/json Content-Length: 24 Server: Werkzeug/0.10.1 Python/2.7.6 Date: 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请求格式,各种功能各种格式,我也是醉了