- FastAPI - Discussion
- FastAPI - Useful Resources
- FastAPI - Quick Guide
- FastAPI - Deployment
- FastAPI - Mounting Flast App
- FastAPI - Middleware
- FastAPI - Mounting A Sub-App
- FastAPI - FastAPI Event Handlers
- FastAPI - Websockets
- FastAPI - Using GraphQL
- FastAPI - Using MongoDB
- FastAPI - SQL Databases
- FastAPI - Crud Operations
- FastAPI - CORS
- FastAPI - Dependencies
- FastAPI - Nested Models
- FastAPI - Response Model
- FastAPI - Header Parameters
- FastAPI - Cookie Parameters
- FastAPI - Uploading Files
- FastAPI - Accessing Form Data
- FastAPI - HTML Form Templates
- FastAPI - Static Files
- FastAPI - Templates
- FastAPI - Request Body
- FastAPI - Pydantic
- FastAPI - Parameter Validation
- FastAPI - Query Parameters
- FastAPI - Path Parameters
- FastAPI - Rest Architecture
- FastAPI - IDE Support
- FastAPI - Type Hints
- FastAPI - Uvicorn
- FastAPI - OpenAPI
- FastAPI - Hello World
- FastAPI - Introduction
- FastAPI - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
FastAPI - Request Body
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.
Expand the node in front of it to reveal the structure of the model
Cpck the Try it out button to fill in the test values in the request body.
Cpck the Execute button and get the server’s response values.
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.
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 −
As it can be seen, college is the path parameter, age is a query parameter, and the Student model is the request body.
Advertisements