-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_path_example.py
54 lines (46 loc) · 1.05 KB
/
url_path_example.py
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
"""
Success -
Request: http://127.0.0.1:8000/items/123?q=datta
Response:{
"item_id": 123,
"q": "datta"
}
Validation examples -
Request: http://127.0.0.1:8000/items/
Response:{
"detail": "Not Found"
}
Request: http://127.0.0.1:8000/items/abc?q=1
Response: {
"detail": [
{
"loc": [
"path",
"item_id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
"""
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
"""
Endpoint with url param (required) and query string param (optional).
FastAPI validates item_id for int value (with the help of type hint)
:param item_id:
:param q:
:return:
"""
return {"item_id": item_id, "q": q}
if __name__ == '__main__':
"""
Run using:
python-fastapi>uvicorn url_path_example:app --reload
http://127.0.0.1:8000 (Use browser)
"""
uvicorn.run(app)