
FastAPI
While HTTP defines standard status codes, you can use custom codes for specific scenarios.
from fastapi import status
# 202 Accepted - Async processing
@app.post("/process", status_code=status.HTTP_202_ACCEPTED)
async def process_async(data: dict):
queue.enqueue(process_task, data)
return {"status": "processing"}
# 206 Partial Content - Partial response
@app.get("/items", status_code=status.HTTP_206_PARTIAL_CONTENT)
async def list_items_partial(limit: int = 10):
return {"items": db.list_items(limit)}
# 429 Too Many Requests - Rate limiting
@app.get("/api/data")
async def get_data():
if rate_limited():
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail="Rate limit exceeded"
)
return {"data": []}from fastapi import status
# Use constants instead of magic numbers
@app.post("/items", status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
return item
# Available constants:
# - HTTP_200_OK
# - HTTP_201_CREATED
# - HTTP_204_NO_CONTENT
# - HTTP_400_BAD_REQUEST
# - HTTP_404_NOT_FOUND
# - HTTP_500_INTERNAL_SERVER_ERROR
# ... and many moreResources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward