mirror of
https://github.com/fastapi/fastapi.git
synced 2026-03-12 20:09:46 -04:00
✨ Add support for Response parameters to set headers, cookies, and status codes (#294)
* ✨ Add support for declaring a Response parameter to set headers and cookies * ✅ Add source for docs and tests * 📝 Add docs for setting headers, cookies and status code * 📝 Add attribution to Hug for inspiring response parameters
This commit is contained in:
committed by
GitHub
parent
c8eea09664
commit
5f7fe926ab
15
docs/src/response_change_status_code/tutorial001.py
Normal file
15
docs/src/response_change_status_code/tutorial001.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from fastapi import FastAPI
|
||||
from starlette.responses import Response
|
||||
from starlette.status import HTTP_201_CREATED
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
tasks = {"foo": "Listen to the Bar Fighters"}
|
||||
|
||||
|
||||
@app.put("/get-or-create-task/{task_id}", status_code=200)
|
||||
def get_or_create_task(task_id: str, response: Response):
|
||||
if task_id not in tasks:
|
||||
tasks[task_id] = "This didn't exist before"
|
||||
response.status_code = HTTP_201_CREATED
|
||||
return tasks[task_id]
|
||||
Reference in New Issue
Block a user