简体   繁体   English

带有正斜杠的 Starlette/FastApi 路由路径组件

[英]Starlette/FastApi route path components with forward slash

I have defined a route in Starlette/FastApi -我在 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我在路径组件中有一些正斜杠,我想发出以下请求以访问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.导致 Starlette/Fastapi 日志中出现 404 错误。

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

How to get the correct path components?如何获得正确的路径组件?

You can use Starlette built-in path convertor您可以使用 Starlette 内置路径转换器

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

Response from Marcelo Trylesinski (Kludex) in fastapi gitter :来自 fastapi gitter 的 Marcelo Trylesinski ( Kludex ) 的回复:

  1. It is not allowed不允许
  2. Temporary solution: don't use path临时解决方案:不要使用路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM