-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic9.py
More file actions
55 lines (47 loc) · 957 Bytes
/
Copy pathtopic9.py
File metadata and controls
55 lines (47 loc) · 957 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Status Codes & Responses
. HTTP status codes
. Custom responses
. Error handling basics
. Interview Questions
"""
from fastapi import FastAPI, status, HTTPException
app = FastAPI()
@app.post("/create_user", status_code= status.HTTP_201_CREATED)
def create_user():
return{
"message":"User Created"
}
"""
# status codes are below -
200 → OK
201 → Created
204 → No Content
400 → Bad Request
401 → Unauthorized
403 → Forbidden
404 → Not Found
422 → Validation Error
500 → Server Error
"""
@app.get("/user")
def get_users():
return{
"status":"Success",
"message":"User Fetched",
"data":{
"name":"Aman",
"age":24
}
}
@app.get("/user/{user_id}")
def get_user(user_id:int):
if user_id != 1:
raise HTTPException(
status_code=404,
detail="User Not Found"
)
return {
"id":1,
"name":"Anu"
}