当前位置: 首页 > news >正文

FastAPI入门:demo、路径参数、查询参数

demo

from fastapi import FastAPIapp = FastAPI()@app.get("/")
async def root():return {"message": "Hello World"}

在终端运行

fastapi dev main.py

结果如下:
在这里插入图片描述
打开http://127.0.0.1:8000:
在这里插入图片描述
交互式API文档:位于http://127.0.0.1:8000/docs
在这里插入图片描述
可选的API文档:位于http://127.0.0.1:8000/redoc
在这里插入图片描述

路径参数

FastAPI 支持使用 Python 字符串格式化语法声明路径参数(变量)

from fastapi import FastAPIapp = FastAPI()@app.get("/items/{item_id}")
async def read_item(item_id):return {"item_id": item_id}

这段代码把路径参数 item_id 的值传递给路径函数的参数 item_id
在这里插入图片描述
路径有顺序之分。例如要使用 /users/me 获取当前用户的数据。然后还要使用 /users/{user_id},通过用户 ID 获取指定用户的数据。
由于路径操作是按顺序依次运行的,因此,一定要在 /users/{user_id} 之前声明 /users/me

预设值

路径操作可以使用 Python 的 Enum 类型接收预设的路径参数

class ModelName(str, Enum):# 多重继承,继承str和Enumalexnet = "alexnet"resnet = "resnet"lenet = "lenet"

包含路径的路径参数

在结尾加上:path表示匹配的是路径

from fastapi import FastAPIapp = FastAPI()@app.get("/files/{file_path:path}")
async def read_file(file_path: str):return {"file_path": file_path}

查询参数

声明的参数不是路径参数时,路径操作函数会把该参数自动解释为查询参数。

可选参数

from fastapi import FastAPIapp = FastAPI()@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str | None = None, short: bool = False):item = {"item_id": item_id}if q:item.update({"q": q})if not short:item.update({"description": "This is an amazing item that has a long description"})return item

将参数默认值设为None即声明可选参数.不声明默认值均为必选参数

布尔类型参数

FastAPI 会按照以下规则进行转换:

转换为 True 的值:
“true” (不区分大小写)
“True”
“TRUE”
“1”
“yes”
“on”
转换为 False 的值:
“false” (不区分大小写)
“False”
“FALSE”
“0”
“no”
“off”
参数不存在时 (使用默认值 False)

from fastapi import FastAPIapp = FastAPI()@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str | None = None, short: bool = False):item = {"item_id": item_id}if q:item.update({"q": q})if not short:item.update({"description": "This is an amazing item that has a long description"})return item

多个路径和查询参数

FastAPI 可以识别同时声明的多个路径参数和查询参数。而且声明查询参数的顺序并不重要。
FastAPI 通过参数名进行检测:

from fastapi import FastAPIapp = FastAPI()@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(user_id: int, item_id: str, q: str | None = None, short: bool = False
):item = {"item_id": item_id, "owner_id": user_id}if q:item.update({"q": q})if not short:item.update({"description": "This is an amazing item that has a long description"})return item

访问:http://127.0.0.1:8000/users/1/items/4?q=4&short=0
在这里插入图片描述

http://www.dtcms.com/a/300100.html

相关文章:

  • Linux进程替换
  • Dynamic Model in RL
  • 渲染篇(二):解密Diff算法:如何用“最少的操作”更新UI
  • 【数据结构】二叉树初阶详解(二):实现逻辑与代码拆解(超详版)
  • 聚类-一种无监督分类算法
  • 中文语音识别与偏误检测系统开发
  • Redis的下载和安装(Linux)
  • 亚马逊全球物流(AGL)与Amazon SEND双重升级:重塑跨境电商物流新生态​
  • FastAPI入门:安装、Pydantic、并发和并行
  • Python应用:三局两胜制石头剪刀布游戏
  • VS Code + LaTeX 绘制电气图完全指南(含 PlantUML 样式参考)
  • 典型的 Vue 3 项目目录结构详解
  • Android中ViewStub和View有什么区别?
  • 过油防溅:3 步搞定 油星乱蹦
  • 具身视觉语言导航算法学习笔记
  • C++20 协程
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-27,(知识点:信号完整性,信号反射,串扰,时延,抖动,衰减)
  • 物联网统一网关:多协议转换与数据处理架构设计
  • useCallback/useMemo
  • Item11:在operator=中处理自我赋值
  • [极客大挑战 2019]FinalSQL--布尔盲注
  • 【web应用】如何进行前后端调试Debug? + 前端JavaScript调试Debug?
  • 内置两大模型,Whisper视频语音转文字,支持批量处理,完全免费!
  • 车载诊断刷写 --- Flash关于擦除和写入大小
  • GStreamer中Element(元素)
  • sendfile系统调用及示例
  • Android 键盘
  • C# 位运算及应用
  • vulhub-earth靶机攻略
  • Day32| 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯