-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi_response_code.py
More file actions
33 lines (25 loc) · 1.53 KB
/
fastapi_response_code.py
File metadata and controls
33 lines (25 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from http import HTTPStatus
from fastapi import FastAPI, status
app = FastAPI()
"""
100 and above are for "Information". You rarely use them directly. Responses with these status codes cannot have
a body.
200 and above are for "Successful" responses. These are the ones you would use the most.
200 is the default status code, which means everything was "OK".
Another example would be 201, "Created". It is commonly used after creating a new record in the database.
A special case is 204, "No Content". This response is used when there is no content to return to the client,
and so the response must not have a body.
300 and above are for "Redirection". Responses with these status codes may or may not have a body, except for 304,
"Not Modified", which must not have one.
400 and above are for "Client error" responses. These are the second type you would probably use the most.
An example is 404, for a "Not Found" response.
For generic errors from the client, you can just use 400.
500 and above are for server errors. You almost never use them directly. When something goes wrong at some part in
your application code, or server, it will automatically return one of these status codes.
"""
# @app.post('/items/', status_code=201)
# @app.post('/items/', status_code=HTTPStatus.CREATED)
@app.post("/items/", status_code=status.HTTP_201_CREATED)
async def create_item(name: str):
return {"name": name}
# PYTHONPATH=module_fastapi uvicorn fastapi_response_code:app --reload