Request Body in FastAPI
Understand how to define a request data model in FastAPI if the client is sending some data to your API.
We'll cover the following
Request data model
We usually need to send the data to an API for processing. This data is called request data. A request body is data sent by the client to your API. A response body is the data that your API sends to the client. The API almost always has to send a response body, but clients don’t necessarily need to send request bodies all the time.
To declare a request body in FastAPI
, we can use Pydantic models with all their power and benefits. If you are not sure of these models, we will see them now.
To send request data, you should use one of the
POST
(commonly used),PUT
,DELETE
orPATCH
methods. Sending a request body with aGET
request has undefined behavior in the specifications. Nevertheless, it is supported byFastAPI
only for very complex/extreme use cases. As it is not a common practice, the interactive docs with Swagger UI won’t show the documentation for the body when usingGET
.
Let us now see how this request body can be taken care of in FastAPI
.
Get hands-on with 1400+ tech skills courses.