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?
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With