Skip to content

๐Ÿ“จ ๐Ÿ‘” ๐Ÿ“Ÿ

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช โœ” ๐Ÿ“จ ๐Ÿท, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ โš™๏ธ ๐Ÿ“จ โฎ๏ธ ๐Ÿ”ข status_code ๐Ÿ™† โžก ๐Ÿ› ๏ธ:

  • @app.get()
  • @app.post()
  • @app.put()
  • @app.delete()
  • โ™’๏ธ.
from fastapi import FastAPI

app = FastAPI()


@app.post("/items/", status_code=201)
async def create_item(name: str):
    return {"name": name}

Note

๐Ÿ‘€ ๐Ÿ‘ˆ status_code ๐Ÿ”ข "๐Ÿ‘จโ€๐ŸŽจ" ๐Ÿ‘ฉโ€๐Ÿ”ฌ (get, post, โ™’๏ธ). ๐Ÿšซ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, ๐Ÿ’– ๐ŸŒ ๐Ÿ”ข & ๐Ÿ’ช.

status_code ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ”ข โฎ๏ธ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ.

Info

status_code ๐Ÿ’ช ๐Ÿ‘ ๐Ÿ“จ IntEnum, โœ… ๐Ÿ http.HTTPStatus.

โšซ๏ธ ๐Ÿ”œ:

  • ๐Ÿ“จ ๐Ÿ‘ˆ ๐Ÿ‘” ๐Ÿ“Ÿ ๐Ÿ“จ.
  • ๐Ÿ“„ โšซ๏ธ โœ… ๐Ÿ—„ ๐Ÿ”— ( & , ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข):

Note

๐Ÿ“จ ๐Ÿ“Ÿ (๐Ÿ‘€ โญ ๐Ÿ“„) ๐ŸŽฆ ๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿ”จ ๐Ÿšซ โœ”๏ธ ๐Ÿ’ช.

FastAPI ๐Ÿ’ญ ๐Ÿ‘‰, & ๐Ÿ”œ ๐Ÿญ ๐Ÿ—„ ๐Ÿฉบ ๐Ÿ‘ˆ ๐Ÿ‡ต๐Ÿ‡ธ ๐Ÿ“ค ๐Ÿ™…โ€โ™‚ ๐Ÿ“จ ๐Ÿ’ช.

๐Ÿ”ƒ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ

Note

๐Ÿšฅ ๐Ÿ‘† โช ๐Ÿ’ญ โšซ๏ธโ” ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ, ๐Ÿšถ โญ ๐Ÿ“„.

๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ”, ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ”ข ๐Ÿ‘” ๐Ÿ“Ÿ 3๏ธโƒฃ 9๏ธโƒฃ ๐Ÿ• ๐Ÿ“จ.

๐Ÿ‘ซ ๐Ÿ‘” ๐Ÿ“Ÿ โœ”๏ธ ๐Ÿ“› ๐Ÿ”— ๐Ÿค” ๐Ÿ‘ซ, โœ‹๏ธ โš  ๐Ÿ• ๐Ÿ”ข.

๐Ÿ“:

  • 100 & ๐Ÿ”› "โ„น". ๐Ÿ‘† ๐Ÿ›Ž โš™๏ธ ๐Ÿ‘ซ ๐Ÿ”—. ๐Ÿ“จ โฎ๏ธ ๐Ÿ‘ซ ๐Ÿ‘” ๐Ÿ“Ÿ ๐Ÿšซ๐Ÿ”œ โœ”๏ธ ๐Ÿ’ช.
  • 200 & ๐Ÿ”› "๐Ÿ†" ๐Ÿ“จ. ๐Ÿ‘ซ ๐Ÿ• ๐Ÿ‘† ๐Ÿ”œ โš™๏ธ ๐Ÿ†.
    • 200 ๐Ÿ”ข ๐Ÿ‘” ๐Ÿ“Ÿ, โ” โ›“ ๐ŸŒ "๐Ÿ‘Œ".
    • โž•1๏ธโƒฃ ๐Ÿ–ผ ๐Ÿ”œ 201, "โœ". โšซ๏ธ ๐Ÿ›Ž โš™๏ธ โฎ๏ธ ๐Ÿ— ๐Ÿ†• โบ ๐Ÿ’ฝ.
    • ๐ŸŽ ๐Ÿ’ผ 204, "๐Ÿ™…โ€โ™‚ ๐ŸŽš". ๐Ÿ‘‰ ๐Ÿ“จ โš™๏ธ ๐Ÿ•โ” ๐Ÿ“ค ๐Ÿ™…โ€โ™‚ ๐ŸŽš ๐Ÿ“จ ๐Ÿ‘ฉโ€๐Ÿ’ป, & ๐Ÿ“จ ๐Ÿ”œ ๐Ÿšซ โœ”๏ธ ๐Ÿ’ช.
  • 300 & ๐Ÿ”› "โŽ". ๐Ÿ“จ โฎ๏ธ ๐Ÿ‘ซ ๐Ÿ‘” ๐Ÿ“Ÿ 5๏ธโƒฃ๐Ÿ“† โš–๏ธ 5๏ธโƒฃ๐Ÿ“† ๐Ÿšซ โœ”๏ธ ๐Ÿ’ช, ๐ŸŒ– 304, "๐Ÿšซ ๐Ÿ”€", โ” ๐Ÿ”œ ๐Ÿšซ โœ”๏ธ 1๏ธโƒฃ.
  • 400 & ๐Ÿ”› "๐Ÿ‘ฉโ€๐Ÿ’ป โŒ" ๐Ÿ“จ. ๐Ÿ‘ซ ๐Ÿฅˆ ๐Ÿ†Ž ๐Ÿ‘† ๐Ÿ”œ ๐ŸŽฒ โš™๏ธ ๐Ÿ†.
    • ๐Ÿ–ผ 404, "๐Ÿšซ ๐Ÿ”Ž" ๐Ÿ“จ.
    • ๐Ÿ’Š โŒ โšช๏ธโžก๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ 400.
  • 500 & ๐Ÿ”› ๐Ÿ’ฝ โŒ. ๐Ÿ‘† ๐ŸŒ– ๐Ÿ™… โš™๏ธ ๐Ÿ‘ซ ๐Ÿ”—. ๐Ÿ•โ” ๐Ÿ•ณ ๐Ÿšถ โŒ ๐Ÿ• ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ“Ÿ, โš–๏ธ ๐Ÿ’ฝ, โšซ๏ธ ๐Ÿ”œ ๐Ÿ” ๐Ÿ“จ 1๏ธโƒฃ ๐Ÿ‘ซ ๐Ÿ‘” ๐Ÿ“Ÿ.

Tip

๐Ÿ’ญ ๐ŸŒ… ๐Ÿ”ƒ ๐Ÿ”  ๐Ÿ‘” ๐Ÿ“Ÿ & โ” ๐Ÿ“Ÿ โšซ๏ธโ”, โœ… ๐Ÿ‡ ๐Ÿงพ ๐Ÿ”ƒ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ.

โŒจ ๐Ÿ’ญ ๐Ÿ“›

โžก๏ธ ๐Ÿ‘€ โฎ๏ธ ๐Ÿ–ผ ๐Ÿ”„:

from fastapi import FastAPI

app = FastAPI()


@app.post("/items/", status_code=201)
async def create_item(name: str):
    return {"name": name}

201 ๐Ÿ‘” ๐Ÿ“Ÿ "โœ".

โœ‹๏ธ ๐Ÿ‘† ๐Ÿšซ โœ”๏ธ โœ โšซ๏ธโ” ๐Ÿ”  ๐Ÿ‘‰ ๐Ÿ“Ÿ โ›“.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿช ๐Ÿ”ข โšช๏ธโžก๏ธ fastapi.status.

from fastapi import FastAPI, status

app = FastAPI()


@app.post("/items/", status_code=status.HTTP_201_CREATED)
async def create_item(name: str):
    return {"name": name}

๐Ÿ‘ซ ๐Ÿช, ๐Ÿ‘ซ ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ ๐ŸŽ ๐Ÿ”ข, โœ‹๏ธ ๐Ÿ‘ˆ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ“‹ ๐Ÿ”Ž ๐Ÿ‘ซ:

๐Ÿ“ก โ„น

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ from starlette import status.

FastAPI ๐Ÿšš ๐ŸŽ starlette.status fastapi.status ๐Ÿช ๐Ÿ‘†, ๐Ÿ‘ฉโ€๐Ÿ’ป. โœ‹๏ธ โšซ๏ธ ๐Ÿ‘Ÿ ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ’ƒ.

๐Ÿ”€ ๐Ÿ”ข

โช, ๐Ÿง ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿฆฎ, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โ” ๐Ÿ“จ ๐ŸŽ ๐Ÿ‘” ๐Ÿ“Ÿ ๐ŸŒ˜ ๐Ÿ”ข ๐Ÿ‘† ๐Ÿ“ฃ ๐Ÿ“ฅ.