Performance: Why request bodies carry structured data
This affects the network payload size and server processing time during API calls, impacting page load and interaction speed.
POST /api/data HTTP/1.1 Content-Type: application/json {"name":"John","age":30,"city":"NY"}
POST /api/data HTTP/1.1 Content-Type: text/plain The user's name is John, age is 30, city is NY
| Pattern | Payload Size | Server Parsing Cost | Network Efficiency | Verdict |
|---|---|---|---|---|
| Unstructured plain text | Larger due to encoding | High due to manual parsing | Low | [X] Bad |
| Structured JSON | Smaller and predictable | Low due to automatic parsing | High | [OK] Good |
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
name: str
age: int
@app.post('/users/')
async def create_user(user: User):
return {"message": f"User {user.name} is {user.age} years old"}{"name": "Alice", "age": 30}from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Product(BaseModel):
id: int
name: str
@app.post('/products/')
def add_product(product: Product):
return productPass the test and this pattern moves unknown