3

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

3
  • Your code works for me right. Latest versions of python 2.7, flask, werkzeug, flask-restful. Maybe you have environment or factory.create_app issues. Commented Apr 25, 2014 at 5:16
  • Thanks for noticing, i tested with a Flask(__name__) instead of factory.create_app and it works for me too. I'm now debugging the factory.create_app function but have no idea where the issue could come from, the function does nothing special ... (I added the function's code to the post) Commented Apr 25, 2014 at 11:31
  • Can't reproduce. I don't have __path__ variable, but probably issue in your config, database or blueprints initialization. Commented Apr 26, 2014 at 5:44

1 Answer 1

1

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 !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.