Request Body
✅ Request Body
When you need to send data from a client (let’s say, a browser) to your API, you send it as a request body.
Request body
data sent by the client to your API
Response body
data your API sends to the client
✅ Import Pydantic’s BaseModel
💡 to declare a request body, you use Pydantic
you need to import BaseModel from pydantic:
✅ Create your data model
decalre a data model as a class
that inherits from BaseModel
1
2
3
4
5
class Person (BaseModel):
name: str
description: str
age: float
level: float
this will decalre a JSON object
like
1
2
3
4
5
6
{
"name": "So Hee",
"description": "Glasses",
"age": 27,
"level": 3,
}
✅ Declare as a paramteter
1
2
3
@app.post("/persons/")
async def create_person(person: Person):
return person
and declare as the type model you created, Person
☑️ Result
FastAPI will
- read the body of the request as
JSON
- Convert the corresponding types and Validate the data
- Give you the recieved data in the parameter
person
✅ Use the model
Inside the function, you can access all the attribute of the model object directly
1
2
3
4
5
6
7
@app.post("/persons/")
async def create_person(person: Person):
person_dict= person.dict()
if person.age:
class = person.age+ person.level
person_dict.update({"class": class})
return person_dict
✅ Request body + path parameters
decalre path parameters and request body at the same time!
- function parameters that match path parameters should be taken from the path
- function parameters that are declared to be Pydantic models should be taken from the request body
1
2
3
@app.put("/persons/{person_id}")
async def create_person(person_id: int, person: Person):
return {"person_id": person_id}
💟 reference
This post is licensed under CC BY 4.0 by the author.