什么是路径

路径参数

就是将路径上的某一部分变成参数,可通过请求传递,然后 FastAPI 解析

上代码

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : test2.py
  4. # @Author: 尘心2259
  5. # @Date : 2022/1/25 23:07
  6. # @Desc :
  7. import uvicorn
  8. from fastapi import FastAPI
  9. app = FastAPI()
  10. @app.get("/items/{item_id}")
  11. async def read_item(item_id):
  12. return {"item_id": item_id}
  13. if __name__ == '__main__':
  14. uvicorn.run(app="test2:app", host="127.0.0.1", port=8000, reload=True, debug=True)

浏览器访问

  1. http://127.0.0.1:8000/items/abcd

有类型的路径参数

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : test2.py
  4. # @Author: 尘心2259
  5. # @Date : 2022/1/25 23:07
  6. # @Desc :
  7. import uvicorn
  8. from fastapi import FastAPI
  9. app = FastAPI()
  10. @app.get("/items/{item_id}")
  11. async def read_item(item_id: int):
  12. return {"item_id": item_id}
  13. if __name__ == '__main__':
  14. uvicorn.run(app="test2:app", host="127.0.0.1", port=8000, reload=True, debug=True)
  • 如果参数不是int类型就会报错

路径函数顺序问题

在创建路径操作时,你会发现有些情况下路径是固定的。

比如 /users/me,我们假设它用来获取关于当前用户的数据.

然后,你还可以使用路径 /users/{user_id} 来通过用户 ID 获取关于特定用户的数据。

由于路径操作是按顺序依次运行的,你需要确保路径 /users/me 声明在路径 /users/{user_id}之前:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : test2.py
  4. # @Author: 尘心2259
  5. # @Date : 2022/1/25 23:07
  6. # @Desc :
  7. import uvicorn
  8. from fastapi import FastAPI
  9. app = FastAPI()
  10. @app.get("/users/me")
  11. async def read_user_me():
  12. return {"user_id": "the current user"}
  13. @app.get("/users/{user_id}")
  14. async def read_item(user_id: str):
  15. return {"user_id": user_id}
  16. if __name__ == '__main__':
  17. uvicorn.run(app="test2:app", host="127.0.0.1", port=8000, reload=True, debug=True)

否则,/users/{user_id} 的路径还将与 /users/me 相匹配,”认为”自己正在接收一个值为 "me"user_id 参数。

枚举类型路径参数

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : test3.py
  4. # @Author: 尘心2259
  5. # @Date : 2022/1/26 22:41
  6. # @Desc :
  7. from enum import Enum
  8. import uvicorn
  9. from fastapi import FastAPI
  10. app = FastAPI()
  11. class ModelFeature(Enum):
  12. name = "尘心"
  13. age = "22"
  14. like = "美女"
  15. @app.get("/models/{model_feature}")
  16. async def get_modle(model_feature: ModelFeature):
  17. if model_feature == ModelFeature.name:
  18. return {"model_feature": model_feature,
  19. "message": "可鲁可🦆"}
  20. if model_feature.value == "22":
  21. return {"modle_feature": model_feature,
  22. "message": "22"}
  23. return {"modle_feature": model_feature,
  24. "message": "美女"}
  25. if __name__ == '__main__':
  26. uvicorn.run(app="test3:app", host="127.0.0.1", port=8000, reload=True, debug=True)

浏览器访问

  1. http://127.0.0.1:8000/models/22

路径转换器

  • 当你有一个路径是 /files/{file_path} ,但是不确定 file_path 到底会取什么值,并不是固定的长度,可能是 /files/home/johndoe/myfile.txt 也可能是 /files/test/myfile.txt ,那怎么办呢?
  • 路径转换器出来啦
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @File : test2.py
  4. # @Author: 尘心2259
  5. # @Date : 2022/1/25 23:07
  6. # @Desc :
  7. import uvicorn
  8. from fastapi import FastAPI
  9. app = FastAPI()
  10. @app.get("/files/{file_path:path}")
  11. async def read_file(file_path: str):
  12. return {"file_path": file_path}
  13. if __name__ == '__main__':
  14. uvicorn.run(app="test2:app", host="127.0.0.1", port=8000, reload=True, debug=True)

浏览器访问

  1. http://127.0.0.1:8000/files/Users/cx2259/Desktop/Fastapi.md
  2. http://127.0.0.1:8000/files/home/test.txt