Python 3.x 使用Flask Restplus,如何创建POST api,而不使用GET使URL相同

Python 3.x 使用Flask Restplus,如何创建POST api,而不使用GET使URL相同,python-3.x,swagger,flask-restful,flask-restplus,Python 3.x,Swagger,Flask Restful,Flask Restplus,我不熟悉Flask和Flask RestPlus。我正在创建一个web api,在这里我想保持我的POSTURL不同于在Swagger中可见的GETURL。例如,在Flask Restplus中 @api.route('/my_api/<int:id>') class SavingsModeAction(Resource): @api.expect(MyApiModel) def post(self): pass #my code goes here

我不熟悉Flask和Flask RestPlus。我正在创建一个web api,在这里我想保持我的
POST
URL不同于在Swagger中可见的
GET
URL。例如,在Flask Restplus中

@api.route('/my_api/<int:id>')
class SavingsModeAction(Resource):
    @api.expect(MyApiModel)
    def post(self):
        pass #my code goes here

    def get(self, id):
        pass #my code goes here
@api.route('/my_-api/'))
类别节省决策(资源):
@expect(MyApiModel)
def post(自我):
通过#我的密码在这里
def get(自我,id):
通过#我的密码在这里
因此,这两个API的url看起来都像

获取:/my_api/{id}

POST:/my_api/{id}

但到目前为止,我的post api中完全没有使用
{id}
部分,这可能会让用户对更新现有记录还是创建新记录产生一些困惑,但是api的目的只是创建。

而不是==>GET:/my_api/{id} 使用查询参数==>GET:/my_api?id= 您上面的代码如下

from flask import request

@api.route('/my_api')
class SavingsModeAction(Resource):
    
    @api.expect(MyApiModel)
    def post(self):
        id = request.args.get("id", type=int)
        ...

    def get(self):
        _id = request.args.get("_id", type=str)
        ...
而不是==>GET:/my_api/{id} 使用查询参数==>GET:/my_api?id= 您上面的代码如下

from flask import request

@api.route('/my_api')
class SavingsModeAction(Resource):
    
    @api.expect(MyApiModel)
    def post(self):
        id = request.args.get("id", type=int)
        ...

    def get(self):
        _id = request.args.get("_id", type=str)
        ...

我在同一个py文件中创建了两个不同的类,因为我的get和post规范大不相同,所以没有必要将它们保留在同一个类中。@IshanBhatt是的,我最终也这样做了。然而,它们是一种相同的集团业务。我在同一个py文件中创建了两个不同的类,因为我的get和post规范大不相同,所以没有必要将它们保留在同一个类中。@IshanBhatt是的,事实上,我最终也这样做了。然而,它们是一种相同的集团业务。