Python FastAPI 多參數傳遞的示例詳解
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
到此這篇關于Python FastAPI 多參數傳遞的文章就介紹到這了,更多相關Python FastAPI 多參數傳遞內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python Django項目和應用的創(chuàng)建詳解
這篇文章主要為大家介紹了Python Django項目和應用的創(chuàng)建,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
python中sqllite插入numpy數組到數據庫的實現方法
本文給大家介紹python中sqllite插入numpy數組到數據庫的實現方法,在文章底部給大家提到了Python 操作sqlite數據庫及保存查詢numpy類型數據的實例代碼,需要的朋友參考下吧2021-06-06
PyTorch、torchvision和Python版本的對應關系
使用PyTorch時,選擇合適的Python版本是至關重要的,錯誤的版本組合可能導致各種兼容性問題,本文就來介紹一下PyTorch、torchvision與Python版本匹配,感興趣的可以了解一下2024-03-03

