Skip to content

Blueprint Dynamic URL For string vs. path #5405

@YTKme

Description

@YTKme

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions