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

fastapi文档

1、获得post所有请求参数
dade: Dict[str, Any]

@app.post("/dade")
async def dade(dade: Dict[str, Any]):return {"dade": dade}

2、获得get,post所以参数


from fastapi import FastAPI,Request```
@app.post("/dade")
async def dade(dade: Dict[str, Any], request: Request)://获得getquery_params = dict(request.query_params)return {"dade": dade,"dddd0":query_params}

获得post参数第二种方法

第一种
dade: Dict[str, Any], 第二种
@app.post("/dade")
async def dade(request: Request):# 获得get参数query_params = dict(request.query_params)# 获取 JSON 格式的 POST 数据json_data = await request.json()return {"dade": json_data,"dddd0":query_params}

3、静态文件目录配置

app.mount("/static", StaticFiles(directory="static"), name="static")

4、热更新,更新代码自动重启

uvicorn main:app --reload

5、路由
分级路由

from fastapi import APIRouter# 创建了分路由
user = APIRouter(prefix="/user", tags=["主项目,顶级,前缀api"])@user.get("/index")
def index():return {"message": "Hello World"}

main.py加

from typing import Dict, Anyfrom fastapi import FastAPI,Request
from starlette.staticfiles import StaticFilesapp = FastAPI()app.mount("/static", StaticFiles(directory="static"), name="static")@app.get("/")
async def root():return {"message": "Hell22o World"}@app.get("/hello/{name}")
async def say_hello(name: str):return {"message": f"Hello {name}"}@app.post("/dade",tags=["给接口分组标签"],summary='测试接口,接口介绍',description='接口的描述',response_description='响应数据的详细描述')
async def dade(request: Request):json_data = await request.json()return {"dade": json_data}from user.index import user
app.include_router(user)# 启动应用(如果直接运行此文件)
if __name__ == "__main__":import uvicornuvicorn.run(app, host="127.0.0.1", port=8000)

在这里插入图片描述
6、路由写到一个文件里
在这里插入图片描述
url.py文件

from fastapi import APIRouter
from user.index import user
urls = APIRouter(prefix="/api")
urls.include_router(user)

index.py文件

from fastapi import APIRouter# 创建了分路由
user = APIRouter(prefix="/user", tags=["主项目,顶级,前缀api"])@user.get("/index")
def index():return {"message": "Hello World"}

main.py文件

from typing import Dict, Anyfrom fastapi import FastAPI,Request
from starlette.staticfiles import StaticFilesapp = FastAPI()app.mount("/static", StaticFiles(directory="static"), name="static")@app.post("/dade",tags=["给接口分组标签"],summary='测试接口,接口介绍',description='接口的描述',response_description='响应数据的详细描述')
async def dade(request: Request):json_data = await request.json()return {"dade": json_data}from urls.url import urls
app.include_router(urls)# 启动应用(如果直接运行此文件)
if __name__ == "__main__":import uvicornuvicorn.run(app, host="127.0.0.1", port=8000)

在这里插入图片描述


文章转载自:

http://1TdDR38i.wtwhj.cn
http://8siZ7iWW.wtwhj.cn
http://bphD8JRb.wtwhj.cn
http://EhBckljS.wtwhj.cn
http://lsOP868z.wtwhj.cn
http://0UZubAxz.wtwhj.cn
http://Ch17AZjH.wtwhj.cn
http://aC5pEgFk.wtwhj.cn
http://qcm6BtYa.wtwhj.cn
http://aCbpkvof.wtwhj.cn
http://EGx7T1Cq.wtwhj.cn
http://88TyocU4.wtwhj.cn
http://AtK1hc1i.wtwhj.cn
http://obmRgdwm.wtwhj.cn
http://V9vlpuCf.wtwhj.cn
http://71FPJ2Yh.wtwhj.cn
http://wJt2Gi64.wtwhj.cn
http://zNOgMtrc.wtwhj.cn
http://jGGRfKzZ.wtwhj.cn
http://WizGbofD.wtwhj.cn
http://5rNix4Bp.wtwhj.cn
http://WlvGiFA4.wtwhj.cn
http://6yHAYUQb.wtwhj.cn
http://gSfSEIdz.wtwhj.cn
http://SgDEhtkB.wtwhj.cn
http://3CrjcMAy.wtwhj.cn
http://WRThcFPw.wtwhj.cn
http://b20CFN7G.wtwhj.cn
http://61DFETnx.wtwhj.cn
http://1CoepTnI.wtwhj.cn
http://www.dtcms.com/a/378093.html

相关文章:

  • vim指令
  • 【源码剖析】4-生产者-KafkaProducer分析
  • 事务方案选型全景图:金融与电商场景的实战差异与落地指南
  • 基于LSTM与3秒级Tick数据的金融时间序列预测实现
  • 第3节-使用表格数据-主键
  • 【C++练习】14.C++统计字符串中字母、数字、空格和其他字符的个数
  • ES6笔记5
  • 协议_https协议
  • 深入 Linux 文件系统:从数据存储到万物皆文件
  • 第十四届蓝桥杯青少组C++选拔赛[2023.1.15]第二部分编程题(1 、求十位数字)
  • CSS 属性概述
  • Ascend310B重构驱动run包
  • 碎片化采购是座金矿:数字化正重构电子元器件分销的价值链
  • 如何配置capacitor 打包的ios app固定竖屏展示?
  • 解锁Roo Code的强大功能:深入理解上下文提及(Context Mentions)
  • BilldDesk:基于Vue3+WebRTC+Nodejs+Electron的开源远程桌面控制
  • 上网管理行为-ISP路由部署
  • 立体校正(Stereo Rectification)的原理
  • 经营帮会员经营:全方位助力企业高效发展,解锁商业新可能
  • 无人机飞控系统原理深度解析
  • 预测赢家-区间dp
  • 2025年- H123-Lc69. x的平方根(技巧)--Java版
  • Visual Studio 2026 震撼发布!AI 智能编程时代正式来临
  • 2023年EAAI SCI1区TOP,基于差分进化的自适应圆柱矢量粒子群优化无人机路径规划,深度解析+性能实测
  • 强化学习框架Verl运行在单块Tesla P40 GPU配置策略及避坑指南
  • HTML 完整教程与实践
  • 前端开发易错易忽略的 HTML 的 lang 属性
  • html中css的四种定位方式
  • GCC 对 C 语言的扩展
  • 基于STM32的智能语音识别饮水机系统设计