Response codes

Notes Single

The response code is set in the route decorator:

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}

Instead of using 'status.HTTP_201_CREATED' you can put the HTTP code directly:

@app.post("/items/", status_code=201)

The response code is set in the route decorator:

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}

Instead of using 'status.HTTP_201_CREATED' you can put the HTTP code directly:

@app.post("/items/", status_code=201)

Thanks for reading :)
I invite you to continue reading other entries and visiting us again soon.

Related Posts: