docs_src/response_change_status_code/tutorial001_py310.py
raw| 1 | from fastapi import FastAPI, Response, status |
| 2 | |
| 3 | app = FastAPI() |
| 4 | |
| 5 | tasks = {"foo": "Listen to the Bar Fighters"} |
| 6 | |
| 7 | |
| 8 | @app.put("/get-or-create-task/{task_id}", status_code=200) |
| 9 | def get_or_create_task(task_id: str, response: Response): |
| 10 | if task_id not in tasks: |
| 11 | tasks[task_id] = "This didn't exist before" |
| 12 | response.status_code = status.HTTP_201_CREATED |
| 13 | return tasks[task_id] |