Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starlette/FastApi route path components with forward slash

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?

like image 883
hangc Avatar asked Feb 08 '26 05:02

hangc


2 Answers

You can use Starlette built-in path convertor

@app.route("/path/{param:path}", name="path-convertor")
like image 106
euri10 Avatar answered Feb 09 '26 18:02

euri10


Response from Marcelo Trylesinski (Kludex) in fastapi gitter:

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

This issue has also been raised on FastApi Github

like image 28
hangc Avatar answered Feb 09 '26 18:02

hangc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!