Flask-Restful驱动的API返回404错误

时间:2014-04-24 21:28:27

标签: python-2.7 flask flask-restful

我在一个项目中使用了Flask-Restful,我也使用Factory模式创建Flask个对象。现在的问题是,当我尝试到达http://localhost:5000/api/v1/user/时,Flask给我404错误,但当我(通过调试器)探索Flask应用对象的url_map时,我的API规则就在那里。所以,如果有人遇到同样的问题,我会采取任何可能的解决方案。

我有以下功能创建API应用程序:

def create_app(settings_override=None):
    """
    Returns the API :class:`Flask` instance.

    :param settings_override: dictionary of settings to override.
    """
    app = factory.create_app(__name__, __path__, settings_override)
    api = Api(app, prefix='/api/v1', catch_all_404s=True)

    # API endpoints connected to the User model.
    api.add_resource(UserAPI, '/user/', endpoint='user')

    return app

UserAPI类的代码(由Flask-Restful使用):

class UserAPI(Resource):
    """
    API :class:`Resource` for returning the details of a user.
    This endpoint can be used to verify a user login credentials.
    """
    def get(self):
        return {'hello': 'world'}, 200

    def post(self):
        pass

factory.create_app功能:

def create_app(package_name, package_path, settings_override=None):
    """
    Returns an instance of Flask configured with common functionnalities for
    Cubbyhole.

    :param package_name: application package name
    :param package_path: application package path
    :param settings_override: a dictionnary of settings to override
    """
    app = Flask(package_name, instance_relative_config=True)

    app.config.from_object('cubbyhole.settings')
    app.config.from_pyfile('settings.cfg', silent=True)
    if settings_override is not None:
        app.config.update(settings_override)

    db.init_app(app)

    register_blueprints(app, package_name, package_path)

    return app

Python 2.7版 烧瓶v。 Flask-Restful版本

1 个答案:

答案 0 :(得分:1)

在对Flask的IRC频道进行一些调查和一些问题后,我发现在使用自定义域名时,应通过SERVER_NAME配置变量设置端口号。因此,问题不是来自工厂代码。

如果您想通过http://myserver.io:5000/访问服务器,请将SERVER_NAME中的端口(此处为5000)设置为SERVER_NAME = myserver.io:5000

我的设置中的这个修改对我有用:)谢谢!