main.py 707 B

12345678910111213141516171819202122232425262728293031323334353637
  1. from fastapi import FastAPI
  2. from pydantic import BaseModel # 参数校验
  3. app = FastAPI()
  4. @app.get("/")
  5. def index():
  6. return {"message": "你已经正确创建api"}
  7. # http://localhost:8000/query?uid=1
  8. @app.get("/query")
  9. def query(uid):
  10. msg = f'uid为{uid}'
  11. return {'success': True, 'msg': msg}
  12. # http://localhost:8000/query/1 参数类型有校验
  13. @app.get("/query/{uid}")
  14. def query(uid: int):
  15. msg = f'uid为{uid}'
  16. return {'success': True, 'msg': msg}
  17. class People(BaseModel):
  18. name: str
  19. age: int
  20. address: str
  21. salary: float
  22. # jsonbody请求
  23. @app.post("/insert")
  24. def insert(people: People):
  25. msg = f"名字:{people.name},年龄{people.age}"
  26. return msg