Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 773
ovg Avatar asked Dec 23 '22 19:12

ovg


1 Answers

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...

like image 97
ovg Avatar answered Dec 28 '22 07:12

ovg