pallets / flask

The Python micro framework for building web applications.

Home Page:https://flask.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blueprint Dynamic URL For `string` vs. `path`

YTKme opened this issue · comments

Context

Not sure if this is a bug or intentional feature. When using Blueprint route with Flask, I understand routes with trailing / can be used to handle routes with and without /. (e.g. /api/do/stuff and /api/do/stuff/.

When you have route:

@blueprint_api.route('/project/<string:category>/', methods=['POST'])
def project_category(category):
    ...

When user enters a route /project/food/, it will return what project_category does and returns, but when user enters /project/food, it will redirect to /project/food/.

So if you don't want the redirect, you might do something like:

@blueprint_api.route('/project/<string:category>', methods=['POST'])  # 200 OK
@blueprint_api.route('/project/<string:category>/', methods=['POST'])  # 200 OK
def project_category(category):
    ...

Replicate

Now this works fine if the dynamic route uses string type, but it seem to be working differently for path type.

@blueprint_api.route('/project/<path:category>', methods=['POST'])  # 308 PERMANENT REDIRECT
@blueprint_api.route('/project/<path:category>/', methods=['POST'])  # 200 OK
def project_category(category):
    ...

With the path type, even when you specify both routes, the route without the trailing / will still redirect to route with trailing / even though it is specified as a route.

Expect

I was expecting the same behavior for both string type dynamic route parameter and path type dynamic route parameter?

Environment: macOS 14.3 (23D56)

  • Python version: 3.10.11
  • Flask version: 3.0.0