网页 综合 更新时间 2026-11-14 05:19:46

Path Parameters - FastAPI

自动蜘蛛 fastapi.tiangolo.com 正文约 3882 字
Use the official FastAPI skill for your installed version. Install it from your project directory:
See Develop with AI.
You can declare path "parameters" or "variables" with the same syntax used by Python format strings:
The value of the path parameter item_id will be passed to your function as the argument item_id.
So, if you run this example and go to http://127.0.0.1:8000/items/foo, you will see a response of:
You can declare the type of a path parameter in the function, using standard Python type annotations:
In this case, item_id is declared to be an int.
This will give you editor support inside of your function, with error checks, completion, etc.
If you run this example and open your browser at http://127.0.0.1:8000/items/3, you will see a response of:
Notice that the value your function received (and returned) is 3, as a Python int, not a string "3".
So, with that type declaration, FastAPI gives you automatic request "parsing".
But if you go to the browser at http://127.0.0.1:8000/items/foo, you will see a nice HTTP error of:
because the path parameter item_id had a value of "foo", which is not an int.
The same error would appear if you provided a float instead of an int, as in: http://127.0.0.1:8000/items/4.2
So, with the same Python type declaration, FastAPI gives you data validation.
Notice that the error also clearly states exactly the point where the validation didn't pass.
This is incredibly helpful while developing and debugging code that interacts with your API.
And when you open your browser at http://127.0.0.1:8000/docs, you will see an automatic, interactive, API documentation like:
Again, just with that same Python type declaration, FastAPI gives you automatic, interactive documentation (integrating Swagger UI).
Notice that the path parameter is declared to be an integer.
And because the generated schema is from the OpenAPI standard, there are many compatible tools.
Because of this, FastAPI itself provides an alternative API documentation (using ReDoc), which you can access at http://127.0.0.1:8000/redoc:
The same way, there are many compatible tools. Including code generation tools for many languages.
All the data validation is performed under the hood by Pydantic, so you get all the benefits from it. And you know you are in good hands.
You can use the same type declarations with str, float, bool and many other complex data types.
Several of these are explored in the next chapters of the tutorial.
When creating path operations, you can find situations where you have a fixed path.
Like /users/me, let's say that it's to get data about the current user.
And then you can also have a path /users/{user_id} to get data about a specific user by some user ID.
Because path operations are evaluated in order, you need to make sure that the path for /users/me is declared before the one for /users/{user_id}:
Otherwise, the path for /users/{user_id} would match also for /users/me, "thinking" that it's receiving a parameter user_id with a value of "me".
Similarly, you cannot redefine a path operation:
The first one will always be used since the path matches first.
If you have a path operation that receives a path parameter, but you want the possible valid path parameter values to be predefined, you can use a standard Python Enum.
Import StrEnum and create a subclass that inherits from it.
StrEnum members are also strings, so the API docs will know that the values must be of type string and will render them correctly.
Then create class attributes with fixed values, which will be the available valid values:
If you are wondering, "AlexNet", "ResNet", and "LeNet" are just names of Machine Learning models.
Then create a path parameter with a type annotation using the enum class you created (ModelName):
Because the available values for the path parameter are predefined, the interactive docs can show them nicely:
访问原文 返回搜索 https://fastapi.tiangolo.com/./tutorial/path-params/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。