Careeroza
Status Codes
Choosing Status Codes
| Code | Meaning | Typical use |
|---|---|---|
| 200 OK | success | GET, PUT |
| 201 Created | resource created | POST |
| 204 No Content | success, empty body | DELETE |
| 400 Bad Request | invalid request | business rule violated |
| 401 / 403 | not authenticated / not allowed | auth |
| 404 Not Found | resource does not exist | GET by id |
| 409 Conflict | state conflict | duplicate email |
| 422 Unprocessable Entity | validation failed | sent automatically by FastAPI |
Setting the Status Code
Example
from fastapi import FastAPI, Response, status
app = FastAPI()
TAGS = {"python", "api"}
@app.post("/tags/{name}", status_code=status.HTTP_201_CREATED)
def add_tag(name: str):
TAGS.add(name)
return {"created": name}
@app.delete("/tags/{name}", status_code=status.HTTP_204_NO_CONTENT)
def delete_tag(name: str):
TAGS.discard(name)
return Response(status_code=status.HTTP_204_NO_CONTENT)Requests and responses
POST /tags/docker
→ 201 {"created": "docker"}
DELETE /tags/docker
→ 204