目錄PythonFastAPI請求參數傳遞FastAPI多參數傳遞類型路徑多參數傳遞GET請求多參數傳遞POST請求多參數傳遞案例完整代碼案例完整測試啟動服務訪問測試GET請求多參數測試POST請求...
Python FastAPI請求參數傳遞
FastAPI多參數傳遞類型
FastAPI通過模板來匹配URL中的參數列表,大致有如下三類方式傳遞參數:
- 路徑參數傳遞:獲取自定義的構造URL中的參數
- GET參數傳遞:獲取一個URL后面帶的
?param1=1¶m2=2
這種類型參數 - POST參數傳遞:獲取POST請求中的參數,因為POST是加密的,因此更加安全,但有額外開銷,測試API使用額外工具或插件或者自己寫Req編程uest等
路徑多參數傳遞
- 訪問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 Qpythonuery
Query(None)里的None是默認值,可以是任意值,當URL里沒有傳入這個參數時,就會用默認值替代;當None為…,則表示為必須傳遞參數
POST請求多參數傳遞
- 訪問url:
http://127.0.0.1:8001/post
body中參數:{“uname”:“goudan”,“sex”:“1”,&ldquowww.newsfordelhi.com;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 FastandroidAPI 多參數傳遞的文章就介紹到這了,更多相關Python FastAPI 多參數傳遞內容請搜索我們以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持我們!
如果認為本文對您有所幫助請贊助本站