已有Flask-restful的api定义:
class TaskAPI(Resource): decorators = [login_required] def get(self, userId, taskId): def post(self, userId, taskId): gLog.debug("self.rootArgs=%s", self.rootArgs) api.add_resource(UserLocationAPI, API_URL_PREFIX + ‘/users/<string:userId>/tasks/<string:taskId>’, endpoint = ‘userTask’) |
但是对于:
想要创建新任务时,taskId为空
以POST方式去访问:
/users/user-bb22f24e-3c27-4e7b-867a-b855e139b295/tasks
/users/user-bb22f24e-3c27-4e7b-867a-b855e139b295/tasks/
都无法执行到上述接口的post中
出现:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p> |
flask-restful post 404 Not Found
Python Flask-Restful POST not taking JSON arguments – Stack Overflow
python 2.7 – Flask-Restful powered API returning 404 errors – Stack Overflow
Error 404 in flask rest api in python – Stack Overflow
python – flask-restful having a get/<id> and post with json in the same class – Stack Overflow
->
API Docs — Flask-RESTful 0.3.5 documentation
去给add_resource,添加多个url:
api.add_resource(TaskAPI, API_URL_PREFIX + ‘/users/<string:userId>/tasks’, API_URL_PREFIX + ‘/users/<string:userId>/tasks/<string:taskId>’,endpoint = ‘userTask’) |
就可以了:
注:
此处之所以这么提示是因为没有提供对应的token。
【总结】
对于Flask-restful中,想要对于
/tasks/<string:taskId>
既要支持get去通过已知taskId获取task信息,
又要支持:
想要去post,创建新的任务,此时taskId为空,访问url是:
/tasks
这种情况,添加支持的话,可以:
在已有的API定义中的add_resource时,多添加一个url:
从:
变成:
api.add_resource(TaskAPI, API_URL_PREFIX + ‘/users/<string:userId>/tasks’, API_URL_PREFIX + ‘/users/<string:userId>/tasks/<string:taskId>’,endpoint = ‘userTask’) |
即可。