我将Flast Restplus用于我的API,并且正在处理Swagger文档。 我将API拆分为多个命名空间,但很难获得正确的网址:
activity.py:
activities_api = Namespace("activities", description="activities of the company", path='/')
class Activity(Resource):
def get(self):
pass
@activities_api.doc(params={'name': 'activity name'})
def post(self):
pass
@activities_api.doc(params={'ID': 'activity ID'})
def delete(self, activity_id):
pass
@activities_api.doc(params={'ID': 'ID de l\'activité'})
def put(self, activity_id):
pass
activities_api.add_resource(Activity, '/activities', endpoint='/')
api.py:
from activities import activities_api
app = Flask( __name__)
api = Api(app, prefix='/v1/', default = None, title='API', version='1.0', catch_all_404s=True)
api.add_namespace(activities_api)
在主页中,我得到以下路径:
PUT /v1/activities POST /v1/activities DELETE /v1/activities GET /v1/activities
当我卷曲到/ v1 / activity时,我得到:
"GET /v1/activities / HTTP/1.1" 404 -
我应该将blueprints
与Namespaces
一起使用吗?
这是我的API的结构:
app.py
__init__.py
activities.py
就这么简单,但是路由是错误的。
答案 0 :(得分:1)
我认为只有一个小错误。
是这一行:
api = Api(app, prefix='/v1/', default = None, title='API', version='1.0', catch_all_404s=True)
请尝试prefix='/v1/'
,而不要说prefix='/v1'
。因为当您将多余的/
添加到末尾时,URL会指向/v1//activities
。