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 FastAPI, we can use Pydantic models with all their power and benefits. If you are not sure of these models, we’ll go over them now.

To send the data body along with the request, we can use one of the POST (commonly used), PUT, DELETE or PATCH methods. When we send a request body with a GET request, it has been observed that there is some undefined behavior. Hence, it is not advised to use GET request normally. Although, FastAPI supports it 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 you use the GET request.

Let’s look at how how this request body can be taken care of in FastAPI.

Get hands-on with 1400+ tech skills courses.