tezvyn:

FastAPI: Set a Response's HTTP Status Code

Source: fastapi.tiangolo.comintermediate

In FastAPI, set the success status code in the decorator, not the function. Use `status_code=201` in `@app.post()` to signal resource creation. The common footgun is placing `status_code` in the function signature instead of the decorator itself.

In FastAPI, you explicitly declare an endpoint's success HTTP status code in its path operation decorator. This separates response configuration from business logic. For example, use `status_code=201` for a POST that creates a resource, overriding the default `200 OK`. The main footgun is misplacing this argument; it belongs in the decorator (`@app.post(...)`), not the path operation function's signature.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

FastAPI: Set a Response's HTTP Status Code · Tezvyn