YouTip LogoYouTip

Fastapi Status Code

HTTP status codes are part of the response and are used to inform the client about the result of the request processing. FastAPI allows you to declare default status codes in the path operation decorator, or modify them dynamically within the function. * * * ## 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 a resource @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 (like `201`) or use constants from `fastapi.status` (recommended, with editor autocomplete 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` | Successful 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` | Internal server error | * * * ## Using status Constants `fastapi.status` provides constants for all HTTP status codes, making it more convenient to use with editor autocomplete: ## 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 is returned, suitable for delete operations. Note that a 204 response cannot contain a response body. * * * ## Common Status Code Constants | Constant | Value | Usage Scenario | | --- | --- | --- | | `status.HTTP_200_OK` | 200 | GET request successful (default) | | `status.HTTP_201_CREATED` | 201 | POST successfully created resource | | `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 invalid token | | `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 (e.g., duplicate creation) | | `status.HTTP_422_UNPROCESSABLE_ENTITY` | 422 | Data validation failed | | `status.HTTP_500_INTERNAL_SERVER_ERROR` | 500 | Internal server error | * * * ## Summary * Declare default status codes using the `status_code` parameter in the decorator * Recommended to use `fastapi.status` constants, which provide editor autocomplete support * Use 201 for POST creating resources, 204 for DELETE deleting resources, and 200 for GET retrieving resources * 4xx indicates client errors, 5xx indicates server errors
← Fastapi File UploadFastapi Body Fields β†’