Vlad Lazar
Vlad Lazar

Reputation: 387

Raise 404 if Flask catch-all route starts with prefix

I am using the catch-all url pattern in my Flask route. I want the view to ignore (throw a 404 error) any path that starts with /api. How can I do this?

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def index(path):
    return 'Hello, World!'

Upvotes: 22

Views: 18923

Answers (1)

davidism
davidism

Reputation: 127200

Check if the path starts with the prefix, then abort if it does.

from flask import abort

if path.startswith('api'):
    abort(404)

Upvotes: 49

Related Questions