12345678910111213141516171819202122232425262728293031323334353637 |
- from fastapi import FastAPI
- from pydantic import BaseModel # 参数校验
- app = FastAPI()
- @app.get("/")
- def index():
- return {"message": "你已经正确创建api"}
- # http://localhost:8000/query?uid=1
- @app.get("/query")
- def query(uid):
- msg = f'uid为{uid}'
- return {'success': True, 'msg': msg}
- # http://localhost:8000/query/1 参数类型有校验
- @app.get("/query/{uid}")
- def query(uid: int):
- msg = f'uid为{uid}'
- return {'success': True, 'msg': msg}
- class People(BaseModel):
- name: str
- age: int
- address: str
- salary: float
- # jsonbody请求
- @app.post("/insert")
- def insert(people: People):
- msg = f"名字:{people.name},年龄{people.age}"
- return msg
|