1

Suppose I have a function decorated with @app.route('/api/<path:path>') and another function decorated with @app.route('/<path:path>'). The url path /api/foo matches against both routes. Does Flask offer a guarantee for which function will be called?

Alessandro Power
  • 2,161
  • 1
  • 11
  • 37

1 Answers1

1

You can find the answer to this in the Flask documentation:

Flask uses the Werkzeug routing system which was designed to automatically order routes by complexity. This means that you can declare routes in arbitrary order and they will still work as expected. This is a requirement if you want to properly implement decorator based routing since decorators could be fired in undefined order when the application is split into multiple modules.

Another design decision with the Werkzeug routing system is that routes in Werkzeug try to ensure that URLs are unique. Werkzeug will go quite far with that in that it will automatically redirect to a canonical URL if a route is ambiguous.

kemis
  • 3,516
  • 5
  • 23
  • 38
  • How does this answer the question? – Raja Simon Jul 17 '17 at 19:52
  • @RajaSimon Please read answer : `which was designed to automatically order routes by complexity` – kemis Jul 17 '17 at 22:03
  • And staff like this you could always test by writing `print("NUMBER 1")` and in the other `print("NUMBER 2")`... – kemis Jul 17 '17 at 22:08
  • @kemis any idea how `complexity` is compared? Is it at all like [CSS specificity points](https://stackoverflow.com/q/2809024/673991)? I was hoping for clues [here](https://werkzeug.palletsprojects.com/en/0.15.x/routing/) but haven't found anything yet. – Bob Stein Jun 12 '19 at 15:21