Reputation: 913
We're using Flask-RESTful to define an API of the form,
bp = Blueprint('api', __name__, url_prefix='/api')
api = Api(bp)
@api.resource('/users/<int:user>')
class User(Resource):
def get(self, user):
...
in conjunction with a Catch-All to render all the pages using React.
bp = Blueprint('index', __name__)
@bp.route('/', defaults={'path': ''})
@bp.route('/<path:path>')
def index(path):
return render_template('index.html')
The issue is requests which don't match a valid API endpoint are supposed to return a 404, however given the Catch-All logic all unregistered API routes simply route to rendering the template.
Is there a good way to ensure that invalid API requests return a 404? There doesn't seem to be a way to exclude routes from the Catch-All so my current workaround is to define something like,
from werkzeug.routing import NotFound
@api.resource('/<path:path>')
class Endpoint(Resource):
def get(self, path):
raise NotFound()
def put(self, path):
raise NotFound()
def post(self, path):
...
which seems a little verbose.
Upvotes: 0
Views: 686
Reputation: 913
It seems like simply overriding the dispatch_request
method is suffice.
@api.resource('/<path:path>')
class Endpoint(Resource):
def dispatch_request(self, *args, **kwargs):
raise NotFound()
Upvotes: 2