
FastAPI
Explicitly setting status codes in your responses tells clients exactly what happened.
from fastapi import status
@app.post("/items", status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
return db.create(item)# GET returns 200
@app.get("/items/{item_id}")
async def get_item(item_id: int):
return item
# POST returns 201
@app.post("/items", status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
return item
# PUT returns 200
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
return item
# DELETE returns 204
@app.delete("/items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_item(item_id: int):
return Nonefrom fastapi import HTTPException
@app.get("/items/{item_id}")
async def get_item(item_id: int):
item = db.get(item_id)
if not item:
raise HTTPException(
status_code=404,
detail="Item not found"
)
return itemResources
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