Starlette/FastApi route path components with forward slash

Viewed 3000

I have defined a route in Starlette/FastApi -

@router.post("/{part}")
def post_method(part):
    return "ok"

@router.post("/{part}/{another_part}")
def another_post_method(part, another_part):
    return "ok"

I have some forward slash in the path components, and I want to make the following request to access post_method

curl -X POST "http://127.0.0.1:5000/api/path%2Fpath" -H "accept: application/json" -d ""

results in the 404 error in the Starlette/Fastapi logs.

INFO: 127.0.0.1:50233 - "POST /api/path/path HTTP/1.1" 404

How to get the correct path components?

2 Answers

You can use Starlette built-in path convertor

@app.route("/path/{param:path}", name="path-convertor")

Response from Marcelo Trylesinski (Kludex) in fastapi gitter:

  1. It is not allowed
  2. Temporary solution: don't use path
Related