Skip to content

๐Ÿ“จ - ๐Ÿ”€ ๐Ÿ‘” ๐Ÿ“Ÿ

๐Ÿ‘† ๐ŸŽฒ โœ โญ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ‘” ๐Ÿ“Ÿ.

โœ‹๏ธ ๐Ÿ’ผ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐ŸŽ ๐Ÿ‘” ๐Ÿ“Ÿ ๐ŸŒ˜ ๐Ÿ”ข.

โš™๏ธ ๐Ÿ’ผ

๐Ÿ–ผ, ๐ŸŒˆ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ "๐Ÿ‘Œ" 200 ๐Ÿ”ข.

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ’ฝ ๐Ÿšซ ๐Ÿ”€, ๐Ÿ‘† ๐Ÿ’š โœ โšซ๏ธ, & ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ "โœ" 201.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ’ช โ›ฝ & ๐Ÿ—œ ๐Ÿ’ฝ ๐Ÿ‘† ๐Ÿ“จ โฎ๏ธ response_model.

๐Ÿ“š ๐Ÿ’ผ, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Response ๐Ÿ”ข.

โš™๏ธ Response ๐Ÿ”ข

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ†Ž Response ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข (๐Ÿ‘† ๐Ÿ’ช ๐Ÿช & ๐ŸŽš).

& โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช โš’ status_code ๐Ÿ‘ˆ ๐Ÿ”€ ๐Ÿ“จ ๐ŸŽš.

from fastapi import FastAPI, Response, status

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 = status.HTTP_201_CREATED
    return tasks[task_id]

& โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ™† ๐ŸŽš ๐Ÿ‘† ๐Ÿ’ช, ๐Ÿ‘† ๐Ÿ›Ž ๐Ÿ”œ ( dict, ๐Ÿ’ฝ ๐Ÿท, โ™’๏ธ).

& ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“ฃ response_model, โšซ๏ธ ๐Ÿ”œ โš™๏ธ โ›ฝ & ๐Ÿ—œ ๐ŸŽš ๐Ÿ‘† ๐Ÿ“จ.

FastAPI ๐Ÿ”œ โš™๏ธ ๐Ÿ‘ˆ ๐Ÿ”€ ๐Ÿ“จ โš— ๐Ÿ‘” ๐Ÿ“Ÿ (๐Ÿช & ๐ŸŽš), & ๐Ÿ”œ ๐Ÿšฎ ๐Ÿ‘ซ ๐Ÿ ๐Ÿ“จ ๐Ÿ‘ˆ ๐Ÿ”Œ ๐Ÿ’ฒ ๐Ÿ‘† ๐Ÿ“จ, โ›ฝ ๐Ÿ™† response_model.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ Response ๐Ÿ”ข ๐Ÿ”—, & โš’ ๐Ÿ‘” ๐Ÿ“Ÿ ๐Ÿ‘ซ. โœ‹๏ธ โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐Ÿ 1๏ธโƒฃ โš’ ๐Ÿ”œ ๐Ÿ†.