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/Status Codes Overview

HTTP Status Codes Overview 📊

Status codes are three-digit numbers that tell the client what happened with their request. Understanding them is essential for API development.

Status Code Categories

Status codes are grouped into five categories:

  • **1xx (Informational)**: Request received, continuing process
  • **2xx (Success)**: Request succeeded
  • **3xx (Redirection)**: Further action needed
  • **4xx (Client Error)**: Request has an error
  • **5xx (Server Error)**: Server failed to fulfill request

Common Status Codes

CodeMeaningWhen to Use
200OKRequest succeeded
201CreatedNew resource created
204No ContentSuccess, no body
400Bad RequestInvalid request
401UnauthorizedAuthentication required
403ForbiddenAccess denied
404Not FoundResource doesn't exist
500Server ErrorServer problem

Using Status Codes in FastAPI

from fastapi import status

@app.get("/items/{item_id}", status_code=status.HTTP_200_OK)
async def get_item(item_id: int):
    return {"id": item_id}

@app.post("/items", status_code=status.HTTP_201_CREATED)
async def create_item(item: dict):
    return item

@app.delete("/items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_item(item_id: int):
    return None

Real-World Example

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    user = db.get_user(user_id)
    if not user:
        # 404 if not found
        raise HTTPException(status_code=404, detail="User not found")
    # 200 if found
    return user

🔑 Key Takeaways

  • ✅ Status codes indicate request outcome
  • ✅ 2xx = success, 4xx = client error, 5xx = server error
  • ✅ Use specific codes for clarity
  • ✅ Codes guide client behavior

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