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/Success Codes

Success Status Codes (2xx) ✅

Success codes indicate that the request was successful. They range from 200-299.

Common Success Codes

200 OK

The request succeeded and returned data.

@app.get("/items/{item_id}")
async def get_item(item_id: int):
    # Default - 200 OK
    return {"id": item_id, "name": "Item"}

201 Created

A new resource was created successfully.

@app.post("/users", status_code=201)
async def create_user(user: dict):
    # 201 indicates a new resource was created
    return {"id": 1, **user}

204 No Content

The request succeeded but there's no content to return.

@app.delete("/users/{user_id}", status_code=204)
async def delete_user(user_id: int):
    # Delete successful, no response body
    return None

Success Code Comparison

CodeNameUsage
200OKRequest succeeded, returning data
201CreatedNew resource created
204No ContentSuccess but no data to return
206Partial ContentPartial response (ranges)

Using Status Codes

from fastapi import status

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

# Return data with custom status
@app.post("/process", status_code=status.HTTP_202_ACCEPTED)
async def process_data(data: dict):
    # 202 = request accepted but still processing
    return {"status": "processing"}

🔑 Key Takeaways

  • ✅ 200 = OK (most common)
  • ✅ 201 = Created (new resource)
  • ✅ 204 = No Content (success, no data)
  • ✅ Use status_code parameter in decorators
  • ✅ 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