我的 api 中的映射不起作用。我有一个带有端点登录的 api。当有 GET 时,返回值是一条消息。我收到 404 错误。
这三个文件是 apis.py、sign_in.py 和 run.py
在 apis.py 文件中定义了所有路由。在sign_in.py中定义了GET和POST。 run.py 中是应用程序的启动。
api.py
api_blueprint = Blueprint('api', __name__)
api = Api(api_blueprint)
# Route
api.add_namespace(sign_in_namespace,'/sign-in')
api.add_namespace(user_namespace,'/<string:username>')
api.add_namespace(AccessPointResource_namespace,'/access-points')
api.add_namespace(NewAddedAccessPointResource_namespace,'/access-points/new')
api.add_namespace(AllAccessPointResource_namespace,'/access-points/all')
api.add_namespace(AccessPointResourceById_namespace,'/access-points/<int:id>')
api.add_namespace(AccessPointResourceByIdWithData_namespace,'/access-points/<id>/data')
api.add_namespace(all_data_acquisition_namespace,'/data/all')
api.add_namespace(by_id_data_acquisition_namespace,'/data/<id>')
sign_in.py
sign_in_namespace = Namespace('sign-in', description='sign in page')
@sign_in_namespace.route("")
class SignInResource(Resource):
def get(self):
return {"message": "Retrieves the sign in resource"}
def post(self):
return {"message": "Signs the user in, create access token and check if user has the correct policy to access the application"}
我期望在sign_in.py中定义消息,但我从swagger收到以下消息。
<!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>
最佳答案
我发现了问题。我必须添加path='sign-in'
在sign_in_namespace = Namespace('sign-in', path='sign-in', description='sign in page')
关于python - 如何使用flask api进行映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57954278/