网页
综合
更新时间 2026-11-17 03:09:21
Body - Multiple Parameters - FastAPI
自动蜘蛛
fastapi.tiangolo.com
正文约 3306 字
Use the official FastAPI skill for your installed version. Install it from your project directory:
See Develop with AI.
Now that we have seen how to use Path and Query, let's see more advanced uses of request body declarations.
First, of course, you can mix Path, Query and request body parameter declarations freely and FastAPI will know what to do.
And you can also declare body parameters as optional, by setting the default to None:
Prefer to use the Annotated version if possible.
Notice that, in this case, the item that would be taken from the body is optional. As it has a None default value.
In the previous example, the path operations would expect a JSON body with the attributes of an Item, like:
But you can also declare multiple body parameters, e.g. item and user:
In this case, FastAPI will notice that there is more than one body parameter in the function (there are two parameters that are Pydantic models).
So, it will then use the parameter names as keys (field names) in the body, and expect a body like:
Notice that even though the item was declared the same way as before, it is now expected to be inside of the body with a key item.
FastAPI will do the automatic conversion from the request, so that the parameter item receives its specific content and the same for user.
It will perform the validation of the compound data, and will document it like that for the OpenAPI schema and automatic docs.
The same way there is a Query and Path to define extra data for query and path parameters, FastAPI provides an equivalent Body.
For example, extending the previous model, you could decide that you want to have another key importance in the same body, besides the item and user.
If you declare it as is, because it is a singular value, FastAPI will assume that it is a query parameter.
But you can instruct FastAPI to treat it as another body key using Body:
Prefer to use the Annotated version if possible.
In this case, FastAPI will expect a body like:
Again, it will convert the data types, validate, document, etc.
Of course, you can also declare additional query parameters whenever you need, additional to any body parameters.
As, by default, singular values are interpreted as query parameters, you don't have to explicitly add a Query, you can just do:
Prefer to use the Annotated version if possible.
Body also has all the same extra validation and metadata parameters as Query, Path and others you will see later.
Let's say you only have a single item body parameter from a Pydantic model Item.
By default, FastAPI will then expect its body directly.
But if you want it to expect a JSON with a key item and inside of it the model contents, as it does when you declare extra body parameters, you can use the special Body parameter embed:
Prefer to use the Annotated version if possible.
In this case FastAPI will expect a body like:
You can add multiple body parameters to your path operation function, even though a request can only have a single body.
But FastAPI will handle it, give you the correct data in your function, and validate and document the correct schema in the path operation.
You can also declare singular values to be received as part of the body.
And you can instruct FastAPI to embed the body in a key even when there is only a single parameter declared.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。