Question

I used Flask-Restful in a project where i also use the Factory pattern to create Flask objects. The problem now is that Flask give me 404 error when i try to reach http://localhost:5000/api/v1/user/ but when i explore (via the debugger) the Flask app object's url_map, my API rule is there. So, if someone ever had the same issue, i'm taking whatever possible solution.

I have the following function creating the API app:

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

The code of UserAPI class (used by 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

The factory.create_app function:

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 version 2.7 Flask v. Flask-Restful version

Was it helpful?

Solution

After some investigations and some questions on Flask's IRC channel, i found that when using custom domain name, the port number should be set via the SERVER_NAME config variable. So, the issue was not coming from the factory code.

If you want to access the server via http://myserver.io:5000/, you set the port, here 5000, in SERVER_NAME as SERVER_NAME = myserver.io:5000.

This single modification in my settings worked for me :) Thanks !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top