Ojasa Mirai

Ojasa Mirai

FastAPI

Loading...

Learning Level

🟢 Beginner🔵 Advanced
🚀 HTTP Status Codes📚 2xx Success Codes📚 4xx Client Errors📚 5xx Server Errors📚 Returning Status Codes📚 Custom Status Codes📚 Status Code Best Practices
Fastapi/Status Codes/Custom Status

Custom Status Codes 🎨

While HTTP defines standard status codes, you can use custom codes for specific scenarios.

Common Custom 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": []}

Status Code Constants

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 more

🔑 Key Takeaways

  • ✅ Use status constants for clarity
  • ✅ 202 for async processing
  • ✅ 206 for partial responses
  • ✅ 429 for rate limiting

Resources

Python Docs

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

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn