11

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?

hangc
  • 4,730
  • 10
  • 33
  • 66

2 Answers2

15

You can use Starlette built-in path convertor

@app.route("/path/{param:path}", name="path-convertor")
euri10
  • 2,446
  • 3
  • 24
  • 46
2

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

hangc
  • 4,730
  • 10
  • 33
  • 66
  • This link is now broken, so it’s hard to understand why using `{param:path}` should be avoided especially given that it’s in the [official FastAPI documentation](https://fastapi.tiangolo.com/tutorial/path-params/#path-convertor). – bfontaine Apr 17 '23 at 13:45
  • 1
    @bfontaine You can follow the issue on the FastApi Github - https://github.com/tiangolo/fastapi/discussions/7328 – hangc Apr 21 '23 at 04:04