Fastapi Status Code 2
HTTP status codes are part of the response, used to inform the client of the request processing result. FastAPI allows you to declare default status codes in path operation decorators, and also dynamically modify them within functions.
* * *
## Declaring Status Codes
Use the `status_code` parameter in the decorator to declare the response status code:
## Example
from fastapi import FastAPI, status
app = FastAPI()
# Use 201 status code when creating resources
@app.post("/items/", status_code=status.HTTP_201_CREATED)
async def create_item(name: str):
return{"name": name}
> `status_code` is a parameter of the decorator method (such as `@app.post()`), not a parameter of the path operation function. It can be a number (such as `201`), or a constant from `fastapi.status` (recommended, with editor auto-completion support).
* * *
## HTTP Status Code Quick Reference
| Range | Category | Common Status Codes | Meaning |
| --- | --- | --- | --- |
| 1xx | Informational | -- | Rarely used directly |
| 2xx | Success | `200 OK` | Request successful (default status code) |
| `201 Created` | Resource created successfully |
| `204 No Content` | Success but no content returned |
| 3xx | Redirection | `304 Not Modified` | Resource not modified (cache valid) |
| 4xx | Client Error | `400 Bad Request` | Request format error |
| `401 Unauthorized` | Not authenticated |
| `403 Forbidden` | Authenticated but no permission |
| `404 Not Found` | Resource does not exist |
| 5xx | Server Error | `500 Internal Server Error` | Server internal error |
* * *
## Using status Constants
`fastapi.status` provides constants for all HTTP status codes, which are more convenient to use with editor auto-completion:
## Example
from fastapi import FastAPI, status
app = FastAPI()
# POST - Create resource, return 201
@app.post("/items/", status_code=status.HTTP_201_CREATED)
async def create_item(name: str):
return{"name": name}
# DELETE - Delete resource, return 204 (no content)
@app.delete("/items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_item(item_id: int):
# Delete operations usually do not return content
pass
# GET - Get resource, return 200 (default)
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return{"item_id": item_id}
> `204 No Content` indicates success but no content returned, suitable for delete operations. Note that 204 responses cannot contain a response body.
* * *
## Common Status Code Constants
| Constant | Value | Use Case |
| --- | --- | --- |
| `status.HTTP_200_OK` | 200 | GET request successful (default) |
| `status.HTTP_201_CREATED` | 201 | POST create resource successful |
| `status.HTTP_204_NO_CONTENT` | 204 | DELETE successful, no content returned |
| `status.HTTP_400_BAD_REQUEST` | 400 | Request parameter error |
| `status.HTTP_401_UNAUTHORIZED` | 401 | Not logged in or token invalid |
| `status.HTTP_403_FORBIDDEN` | 403 | No permission to access |
| `status.HTTP_404_NOT_FOUND` | 404 | Resource does not exist |
| `status.HTTP_409_CONFLICT` | 409 | Resource conflict (such as duplicate creation) |
| `status.HTTP_422_UNPROCESSABLE_ENTITY` | 422 | Data validation failed |
| `status.HTTP_500_INTERNAL_SERVER_ERROR` | 500 | Server internal error |
* * *
## Summary
* Use the `status_code` parameter of the decorator to declare the default status code
* Recommended to use `fastapi.status` constants, with editor auto-completion support
* POST to create resources uses 201, DELETE to delete resources uses 204, GET to retrieve resources uses 200
* 4xx indicates client errors, 5xx indicates server errors
YouTip