English 中文(简体)
FastAPI - Request Body
  • 时间:2024-12-22

FastAPI - Request Body


Previous Page Next Page  

We shall now use the Pydantic model object as a request body of the cpent’s request. As mentioned earper, we need to use POST operation decorator for the purpose.


import uvicorn
from fastapi import FastAPI
from typing import List
from pydantic import BaseModel, Field
app = FastAPI()
class Student(BaseModel):
   id: int
   name :str = Field(None, title="name of student", max_length=10)
   subjects: List[str] = []
@app.post("/students/")
async def student_data(s1: Student):
   return s1

As it can be seen, the student_data() function is decorated by @app.post() decorator having the URL endpoint as "/students/". It receives an object of Student class as Body parameter from the cpent’s request. To test this route, start the Uvicorn server and open the Swagger UI documentation in the browser by visiting http://localhost:8000/docs

The documentation identifies that "/students/" route is attached with student_data() function with POST method. Under the schemas section the Student model will be psted.

FastAPI Request Body

Expand the node in front of it to reveal the structure of the model

FastAPI Request Body

Cpck the Try it out button to fill in the test values in the request body.

FastAPI Request Body

Cpck the Execute button and get the server’s response values.

FastAPI Request Body

While a Pydantic model automatically populates the request body, it is also possible to use singular values to add attributes to it. For that purpose, we need to use Body class objects as the parameters of the operation function to be decorated.

First, we need to import Body class from fastapi. As shown in the following example, declare name and marks as the Body parameters in the definition of student_data() function below the @app.post() decorator.


import uvicorn
from fastapi import FastAPI, Body
@app.post("/students")
async def student_data(name:str=Body(...),
marks:int=Body(...)):
   return {"name":name,"marks": marks}

If we check the Swagger UI documentation, we should be able to find this POST method associated to student_data() function and having a request body with two parameters.

FastAPI Request Body

It is also possible to declare an operation function to have path and/or query parameters along with request body. Let us modify the student_data() function to have a path parameter college’, age as query parameter and a Student model object as body parameter.


@app.post("/students/{college}")
async def student_data(college:str, age:int, student:Student):
   retval={"college":college, "age":age, **student.dict()}
   return retval

The function adds values of college and age parameters along with the dictionary representation of Student object and returns it as a response. We can check the API documentation as follows −

FastAPI Request Body

As it can be seen, college is the path parameter, age is a query parameter, and the Student model is the request body.

Advertisements