ovg
ovg

Reputation: 1546

404 when accessing resource Flask-Restful

from flask import g, request, session, render_template, flash, redirect, url_for
from flask import current_app as app

@app.route('/')
def home():
    return 'this works'


from flask_restful import Resource, Api
from app.extensions import api


class HelloWorld(Resource):
    def get(self):
        return {'Hello': 'World'}

api.add_resource(HelloWorld, '/test')  # Getting a 404 for this route on http://127.0.0.1:5000/test

Extensions sets up the api variable:

api = Api()
api.init_app(app)

I cannot figure out why I get a 404 when trying to access an api resource?

Upvotes: 4

Views: 6390

Answers (1)

ovg
ovg

Reputation: 1546

Ok, the problem seems to be that the below ordering is wrong. I must add resources before I init the api.

api = Api()
api.init_app(app)
api.add_resource(HelloWorld, '/')

Fix:

api = Api()
api.add_resource(HelloWorld, '/')
api.init_app(app)

This is quite strange given that e.g. SQLAlchemy needs to call init_app before it is used...

Upvotes: 12

Related Questions