最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask-Restful中对于接口无法实现不传递参数时执行post

Flask crifan 5347浏览 0评论

已有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’)

即可。

转载请注明:在路上 » 【已解决】Flask-Restful中对于接口无法实现不传递参数时执行post

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.165 seconds, using 22.19MB memory