python - 如何使用flask api进行映射

标签 python api flask mapping

我的 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/

相关文章:

docker - 运行 docker 容器时出错 : starting container process caused "exec:\"python\": executable file not found in $PATH": unknown

Python websocket 连接并监听传入消息

javascript - 无法访问本地 Node 服务器上创建的API

javascript - Wistia API 播放/暂停按钮

python - 将用户构建的 json 编码器传递给 Flask 的 jsonify

python - flask-login 仅在 get_id() 返回 self.email 时有效

python - 使用正则表达式(python)挑选出重复模式

python - Django:HTML 标签显示为文本

Python 尝试解析输入

javascript - RapidAPI 响应体显示为 "undefined"