from fastapi import FastAPI
app = FastAPI()
@app.post('/ints')
def post_int(x: int, y: int):
return x, y
@app.post('/lists')
def post_list(x: list[int], y: list[int]):
return x, y
rrequests.py
import requests
r = requests.post('http://127.0.0.1:8000/ints', json={'x': 1, 'y': 2})
print(r.json()) # returns an error saying that x and y are missing
r = requests.post('http://127.0.0.1:8000/ints?x=1&y=2')
print(r.json()) # returns the two ints
r = requests.post('http://127.0.0.1:8000/lists', json={'x': [1, 2, 5], 'y': [1, 2, 3]})
print(r.json()) # returns the two lists
启动服务器,在终端运行“ FastApi Dev/Path/path/path/to/endpoints.py”。在另一个终端中运行requests.py。
当您注册一个端点时
@app.post('/ints')
def post_int(x: int, y: int):
return x, y
在fastapi中,添加端点
decorator (routing.py)
add_api_route (routing.py)
__init__ (routing.py)
get_dependant (dependencies/utils.py)
analyze_param (dependencies/utils.py)
analyze_param
您可以看到标量参数具有字段信息类型查询。