【问题标题】:how to fix Flask RESTful api server endpoint failing problem?如何修复 Flask RESTful api 服务器端点失败问题?
【发布时间】:2020-04-15 16:54:14
【问题描述】:

我是使用 openapi sepc 进行 api 开发的新手,所以我关注了this blog,我只是使用服务器工件来理解代码。我在简单的工作中使用了/openapi/photo_album/codegen_server。为此,我下载了文件并尝试在默认服务器端点上运行服务器。我安装了所有必需的依赖项并点击了python -m openapi_server,但浏览器端点总是用以下错误消息困扰我:

{ "detail": "在服务器上没有找到请求的 URL。如果你 手动输入网址,请检查拼写并重试。",
“状态”:404,“标题”:“未找到”,“类型”:“关于:空白”}

我的尝试

这里是sourcecode that I tried

我还尝试了以下方法:

import connexion

from openapi_server import encoder

def main():
    app = connexion.App(__name__, specification_dir='./openapi/')
    app.app.json_encoder = encoder.JSONEncoder
    app.add_api('openapi.yaml', arguments={'title': 'Sample OpenAPI Specification'})
    app.run(host='127.0.0.1',port=5000, debug=True)


if __name__ == '__main__':
    main()

我尝试关闭防火墙并尝试访问服务器端点,但仍然无法正常工作。我正在为我的系统使用 windows-x64,这是我第一次尝试使用 api 开发。我不知道发生了什么以及如何解决我的问题。甚至我在 cmd 上尝试了 route print -4ping 127.0.0.1 并尝试使用本地 ipv4 地址但仍然无法正常工作。我的意图是向默认服务器端点发出请求并进行基本的 api 测试。谁能提供可能的解决方案如何解决此错误?任何快速的解决方案?谢谢

更新

我在我的 windows 机器上尝试了任何烧瓶休息 api 示例项目,它们都不起作用,服务器端点总是失败。那是因为我的本地机器设置吗?这个错误正在杀死我,如何解决这个问题?任何快速的解决方案?谢谢

【问题讨论】:

    标签: python openapi connexion


    【解决方案1】:

    我通过在requirements 中更新connexion==2.6.0 解决了这个问题。同样为hostport 使用不同的值会在默认端点上运行服务器。这个source code 有一些问题,因为代码是由 openapi 代码生成器生成的,不能保证示例 api 服务器运行。我最近对 ​​api 开发的观察是使用 flask-restplus,它在服务器端点上带有漂亮的 UI api 视图。

    这是使用flask-restplus的测试api,希望对flask-restplus API开发的新手有用。

    from flask import Flask
    from flask_restplus import Api, fields, Resource, marshal
    
    app = Flask(__name__)
    api = Api()
    api.init_app(app)
    
    metadata_model = api.model("metadata", {
        'file': fields.String()
    })
    
    user_model = api.model('UserModel', {
              "user_id": fields.Integer(required=True, description=''),
              "user_name": fields.String(required=True, description=''),
              "user_role": fields.String(required=False, description='')
    })
    
    response_model = api.model("Result", {
        'metadata': fields.List(fields.Nested(metadata_model)),
        'result': fields.Raw()
    })
    
    
    @api.route("/test")
    class ApiView(Resource):
    
        @api.marshal_with(response_model)
        def get(self):
    
            data = {'metadata': {},
                    'result': self.get_user()}
            return data
    
    
        def get_user(self):
            # Access database and get data
            user_data = [{'user_id': 1, 'user_name': 'John', 'user_role': 'editor'},
                         {'user_id': 2, 'user_name': 'Sue', 'user_role': 'curator'}]
    
            # The kwarg envelope does the trick
            return marshal(user_data, user_model, envelope='data')
    
    
    app.run(host='127.0.0.1', port='8080', debug=True)
    

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 1970-01-01
      • 2017-11-11
      • 2021-06-25
      • 2014-03-06
      • 2019-12-03
      • 2017-10-20
      • 1970-01-01
      • 2018-11-19
      相关资源
      最近更新 更多