问题描述
我是 Flask 和 Flask-RestPlus 的新手.我正在创建一个 web api,我希望我的 POST
url 与 Swagger 中可见的 GET
url 不同.例如在 Flask-Restplus
I'm new to Flask and Flask-RestPlus. I'm creating a web api where I want keep my POST
urls different from the GET
urls which are visible in Swagger. For example in 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
因此,两个 apis url 的 swagger 看起来都像
So in swagger for the both apis url would look like
获取:/my_api/{id}
POST:/my_api/{id}
POST: /my_api/{id}
但到目前为止,我完全没有在我的 post api 中使用 {id}
部分,这可能会给用户带来一些混乱,是更新现有记录还是创建新记录,然而 api 的目的只是为了创建.
But so far I have absolutely no use of {id}
part in my post api and it perhaps creates a bit of confusion for an user whether to update an existing record or to create a new, however the purpose of the api is just to create.
推荐答案
Instead of ==>获取:/my_api/{id}
使用查询参数==>获取:/my_api?id=
你上面的代码就像
Instead of ==> GET: /my_api/{id}
use query params ==> GET: /my_api?id=
your above code will be like
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)
...
这篇关于使用 Flask-Restplus 如何在不使 URL 与 GET 相同的情况下创建 POST api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!