码迷,mamicode.com
首页 > 其他好文 > 详细

flask-restful 请求解析

时间:2015-07-20 12:25:03      阅读:1209      评论:0      收藏:0      [点我收藏+]

标签:

基本参数

from flask import Flask
from flask.ext.restful import reqparse, abort, Api, Resource

app = Flask(__name__)
api = Api(app)

TODOS = { todo1: {task: build an API},
    todo2: {task: ?????},
    todo3: {task: profit!},
}


parser = reqparse.RequestParser()
parser.add_argument(task, type=str, help=Rate cannot be converted)
parser.add_argument(rate, type=int)

def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        abort(404, message="Todo {} doesn‘t exist".format(todo_id))

# TodoList
#   shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
    def get(self):
        return TODOS

    def post(self):
        args = parser.parse_args()
        todo_id = int(max(TODOS.keys()).lstrip(todo)) + 1
        todo_id = todo%i % todo_id
        TODOS[todo_id] = {task: args[task]}
        return TODOS[todo_id], 201

##
## Actually setup the Api resource routing here
##
api.add_resource(TodoList, /todos)

if __name__ == __main__:
    app.run(debug=True)

关键代码

parser = reqparse.RequestParser()
parser.add_argument(task, type=str, help=Rate cannot be converted)
parser.add_argument(rate, type=int)

args = parser.parse_args()

指定了help信息,在解析类型错误的时候,就会作为错误信息呈现出来;否则默认返回类型错误本身的错误。

必须的参数

添加条件

whole2.py

from flask import Flask
from flask.ext.restful import reqparse, abort, Api, Resource

app = Flask(__name__)
api = Api(app)

TODOS = { todo1: {task: build an API},
    todo2: {task: ?????},
    todo3: {task: profit!},
}


parser = reqparse.RequestParser()
parser.add_argument(task, type=str, help=Rate cannot be converted)
parser.add_argument(rate, type=int, required=Ture)

def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        abort(404, message="Todo {} doesn‘t exist".format(todo_id))

# TodoList
#   shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
    def get(self):
        return TODOS

    def post(self):
        args = parser.parse_args()
        todo_id = int(max(TODOS.keys()).lstrip(todo)) + 1
        todo_id = todo%i % todo_id
        TODOS[todo_id] = {task: args[task]}
        return TODOS[todo_id], 201

##
## Actually setup the Api resource routing here
##
api.add_resource(TodoList, /todos)

if __name__ == __main__:
    app.run(debug=True)

关键代码

parser.add_argument(‘rate‘, type=int, required=Ture)

运行

python whole2.py

另一个窗口执行

jihite@ubuntu:~/project/flask$ curl 127.0.0.1:5000/todos -d task=hello -X POST
{
    "message": {
        "rate": "Missing required parameter in the JSON body or the post body or the query string"
    }
}

添加rate参数再执行,就ok了

jihite@ubuntu:~/project/flask$ curl 127.0.0.1:5000/todos -d task=hello -d rate=3 -X POST
{
    "task": "hello"
}

多个值&列表

whole3.py

from flask import Flask
from flask.ext.restful import reqparse, abort, Api, Resource

app = Flask(__name__)
api = Api(app)

TODOS = { todo1: {task: build an API},
    todo2: {task: ?????},
    todo3: {task: profit!},
}


parser = reqparse.RequestParser()
parser.add_argument(task, type=str, action=append)

def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        abort(404, message="Todo {} doesn‘t exist".format(todo_id))

# TodoList
#   shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
    def get(self):
        return TODOS

    def post(self):
        args = parser.parse_args()
        todo_id = int(max(TODOS.keys()).lstrip(todo)) + 1
        todo_id = todo%i % todo_id
        TODOS[todo_id] = {task: args[task]}
        return TODOS[todo_id], 201

##
## Actually setup the Api resource routing here
##
api.add_resource(TodoList, /todos)

if __name__ == __main__:
    app.run(debug=True)

执行

python whole3.py

另一窗口执行

jihite@ubuntu:~/project/flask$ curl 127.0.0.1:5000/todos -d task=hello -d task=hello2 -d task=hello4 -X POST
{
    "task": [
        "hello", 
        "hello2", 
        "hello4"
    ]
}

浏览器打开结果

{
    "todo1": {
        "task": "build an API"
    }, 
    "todo2": {
        "task": "?????"
    }, 
    "todo3": {
        "task": "profit!"
    }, 
    "todo4": {
        "task": [
            "hello", 
            "hello2", 
            "hello4"
        ]
    }
}

参数位置

待解决:http://www.pythondoc.com/Flask-RESTful/reqparse.html#id5

  

  

flask-restful 请求解析

标签:

原文地址:http://www.cnblogs.com/kaituorensheng/p/4661033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!