Python FastAPI 多参数传递的示例详解
作者:Aidon-东哥博客 时间:2023-07-03 01:21:05
Python FastAPI请求参数传递
FastAPI多参数传递类型
FastAPI通过模板来匹配URL中的参数列表,大致有如下三类方式传递参数:
路径参数传递:获取自定义的构造URL中的参数
GET参数传递:获取一个URL后面带的
?param1=1¶m2=2
这种类型参数POST参数传递:获取POST请求中的参数,因为POST是加密的,因此更加安全,但有额外开销,测试API使用额外工具或插件或者自己写Request等
路径多参数传递
访问url:
http://127.0.0.1:8001/uname=goudan/sex=1/age=18
匹配模板:
http://127.0.0.1:8001/uname=值1/sex=值2/age=值3
对应API代码
import uvicorn
from fastapi import FastAPI
#构造FastAPI实例
app = FastAPI(name="mutilParam")
""""
路径多参数传递
访问url:
http://127.0.0.1:8001/uname=goudan/sex=1/age=18
匹配模板:
http://127.0.0.1:8001/uname=值1/sex=值2/age=值3
对应API代码:
"""
@app.get("/uname={uname}/sex={sex}/age={age}")
async def api1(uname: str, sex, age: str):
return {
"uname": uname,
"sex": sex,
"age": age,
}
GET请求多参数传递
访问url:http://127.0.0.1:8001/get?uname=goudan&sex=1&age=18
匹配模板:http://127.0.0.1:8001/get?uname=值1&sex=值2&age=值3
对应API代码
import uvicorn
from fastapi import FastAPI
#构造FastAPI实例
app = FastAPI(name="mutilParam")
""""
GET请求多参数传递
访问url:
http://127.0.0.1:8001/get?uname=goudan&sex=1&age=18
匹配模板:
http://127.0.0.1:8001/get?uname=值1&sex=值2&age=值3
对应API代码:
"""
@app.get("/get")
async def api2(uname=Query(None), sex=Query(...), age=Query(None)):
return {
"uname": uname,
"sex": sex,
"age": age,
}
注意:
参数里uname=Query(None):
uname对应着传入URL里的?uname=xxx
Query()是导的包from fastapi import Query
Query(None)里的None是默认值,可以是任意值,当URL里没有传入这个参数时,就会用默认值替代;当None为…,则表示为必须传递参数
POST请求多参数传递
访问url:
http://127.0.0.1:8001/post
body中参数:{“uname”:“goudan”,“sex”:“1”,“age”:“18”}
匹配模板:
http://127.0.0.1:8001/post
body中参数:{uname=值1, sex=值2, age=值3}
对应API代码
import uvicorn
from fastapi import FastAPI
#构造FastAPI实例
app = FastAPI(name="mutilParam")
""""
POST请求多参数传递
"""
@app.post("/post")
async def api3(uname=Body(None), sex=Body(...), age=Body(None)):
return {
"uname": uname,
"sex": sex,
"age": age,
}
注意:
Post参数传递几乎和GET相似,就是将Query替换成来写,参数里uname=Body(None):
Body()是导的包from fastapi import Body
Body的写法与用法和Query相同,括号里是默认值
uname=Body(None)里的None是默认值,可以是任意值,当URL里没有传入这个参数时,就会用默认值替代;当None为…,则表示为必须传递参数
案例完整代码
三种参数传递API的完整代码如下:
import uvicorn
from fastapi import FastAPI
from fastapi import Query
from fastapi import Body
#构造FastAPI实例
app = FastAPI(name="mutilParam")
""""
路径多参数传递
访问url:
http://127.0.0.1:8001/uname=goudan/sex=1/age=18
匹配模板:
http://127.0.0.1:8001/uname=值1/sex=值2/age=值3
对应API代码:
"""
@app.get("/uname={uname}/sex={sex}/age={age}")
async def api1(uname: str, sex, age: str):
return {
"uname": uname,
"sex": sex,
"age": age,
}
""""
GET请求多参数传递
访问url:
http://127.0.0.1:8001/get?uname=goudan&sex=1&age=18
匹配模板:
http://127.0.0.1:8001/get?uname=值1&sex=值2&age=值3
对应API代码:
参数里uname=Query(None):
uname对应着传入URL里的?uname=xxx
Query()是导的包from fastapi import Query
Query(None)里的None是默认值,可以是任意值,当URL里没有传入这个参数时,就会用默认值替代;当None为...,则表示为必须传递参数
"""
@app.get("/get")
async def api2(uname=Query(None), sex=Query(...), age=Query(None)):
return {
"uname": uname,
"sex": sex,
"age": age,
}
""""
POST请求多参数传递
访问url:
http://127.0.0.1:8001/post
{"uname":"goudan","sex":"1","age":"18"}
匹配模板:
http://127.0.0.1:8001/post
{"uname":"xxx","age":"x"}
对应API代码:
Post参数传递几乎和GET相似,就是将Query替换成来写:
参数里uname=Body(None):
Body()是导的包from fastapi import Body
Body的写法与用法和Query相同,括号里是默认值
uname=Body(None)里的None是默认值,可以是任意值,当URL里没有传入这个参数时,就会用默认值替代;当None为...,则表示为必须传递参数
"""
@app.post("/post")
async def api3(uname=Body(None), sex=Body(...), age=Body(None)):
return {
"uname": uname,
"sex": sex,
"age": age,
}
#主函数
if __name__ == '__main__':
#启动服务
uvicorn.run(app='MutilParamOfAPI:app', host="127.0.0.1", port=8001, reload=False)
案例完整测试
启动服务
在完整代码案例中任何位置—>右键—>Run MutilParamOfAPI—>信息如下:
/Users/liyadong/PycharmProjects/MyAPI/MutilParamOfAPI.py
INFO: Started server process [31526]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
访问测试
路径多参数测试
浏览器访问输入地址栏如下:
http://127.0.0.1:8001/uname=goudan/sex=1/age=18
浏览器页面信息如下:
{"uname":"goudan","sex":"1","age":"18"}
{"uname":"goudan","sex":"1","age":"18"}
GET请求多参数测试
浏览器访问输入地址:http://127.0.0.1:8001/get?uname=goudan&sex=1&age=18
浏览器页面信息如下:
{"uname":"goudan","sex":"1","age":"18"}
POST请求多参数测试
到此测试就完毕了,关于可传参数和必传参数,大家自行减少参数键值对即可。
参考资料:
https://blog.csdn.net/weixin_35757704/article/details/123392281
来源:https://blog.csdn.net/u010839779/article/details/128253025
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
字符编码笔记 ASCII,Unicode和UTF-8
![](https://img.aspxhome.com/file/2023/1/131881_0s.jpg)
python打开windows应用程序的实例
在Python中字典根据多项规则排序的方法
Jupyter notebook命令和编辑模式常用快捷键汇总
使用sqlserver存储过程sp_send_dbmail发送邮件配置方法(图文)
![](https://img.aspxhome.com/file/2023/2/92742_0s.png)
Golang开发之接口的具体使用详解
分享9个好用的Python技巧
Python实现计算最小编辑距离
![](https://img.aspxhome.com/file/2023/0/80630_0s.png)
javascript伸缩型菜单实现代码
![](https://img.aspxhome.com/file/2023/3/56593_0s.png)
详解BurpSuite安装和配置
![](https://img.aspxhome.com/file/2023/0/99390_0s.jpg)
页面制作中要注意的编码问题
python2.7实现FTP文件下载功能
详解从vue的组件传值着手观察者模式
![](https://img.aspxhome.com/file/2023/3/126413_0s.jpg)
如何用ASP实现文章点击数?
jupyter-lab设置自启动及远程连接开发环境
基于Opencv的图像卡通化实现代码
![](https://img.aspxhome.com/file/2023/9/123779_0s.jpg)
Django框架中方法的访问和查找
一文带你了解MySQL中的事务
![](https://img.aspxhome.com/file/2023/4/74794_0s.png)
详解Python中range()与xrange()的区别
商品评论的设计
![](https://img.aspxhome.com/file/UploadPic/20101/11/1-70s.jpg)