如何在python中添加参数到flask-restful?

时间:2014-01-14 05:57:54

标签: python flask flask-restful

如何创建一个类似于:

的api调用
api.add_resource(MyResource, '/myresource/<myurlparameter>')

我不知道如何从内部处理它:

class MyResource(restful.Resource):
    def get(self):
        print myurlparameter
        return ""

另外,我注意到我只能将add_resource添加到一个级别:

api.add_resource(MyResource, '/myresource') # this works
api.add_resource(MyResource, '/myresource/test') # this this not work

1 个答案:

答案 0 :(得分:3)

您可以在docs中找到所需的一切。

Parameter in requests

class TodoSimple(Resource):
    def get(self, todo_id):
        return {todo_id: todos[todo_id]}

    def put(self, todo_id):
        todos[todo_id] = request.form['data']
        return {todo_id: todos[todo_id]}

api.add_resource(TodoSimple, '/<string:todo_id>')

Few endpoints

api.add_resource(HelloWorld,
    '/',
    '/hello')