fastapi有各種各樣的參數(shù),包括:
- url參數(shù)(定義在url中的參數(shù))
- param參數(shù)(使用url后面?xx=xx)定義的參數(shù)
- body參數(shù)(在請(qǐng)求主體中攜帶的json參數(shù))
- form參數(shù)(在請(qǐng)求主體中攜帶的web表單參數(shù))
- cookie參數(shù)(在請(qǐng)求的cookie中攜帶的參數(shù))
- file參數(shù)(客戶端上傳的文件)
1. url參數(shù)
url參數(shù)是一種應(yīng)用非常廣泛的參數(shù),很多地方都會(huì)使用,甚至簡書的博客系統(tǒng)(看看你這篇文章的url)可能同樣應(yīng)用了這種類型的參數(shù)(但有可能不是這種實(shí)現(xiàn)).
他的意思是,將信息放在url中,比如這樣:
http://www.reibang.com/u/91bc345f7c84
那么,我們就可以將后面的91bc345f7c84的部分定義為url參數(shù),傳遞到函數(shù)中:
例:
from fastapi import FastAPI
app=FastAPI()
app.get("/echo/{text}") #注意這里text將是函數(shù)定義的參數(shù)名.
async def getEchoApi(text:str): #通過定義參數(shù)類型可以讓fastapi進(jìn)行默認(rèn)的參數(shù)檢查.
return {"echo":text}
將其用uvicorn放到localhost:8000上執(zhí)行.(不會(huì)的可以參見這篇文章運(yùn)行fastapi程序)
很簡單的一段代碼,將url中的內(nèi)容原樣返回.
比如說,當(dāng)我們?cè)L問這個(gè)url:
localhost:8000/echo/HelloWorld
返回的內(nèi)容將是:
{"echo":text}
2. param參數(shù)
這可能時(shí)最常見的一種參數(shù)了,比如說,當(dāng)我們百度時(shí),可以在地址欄上看到這樣的地址:
https://www.baidu.com/s?ie=UTF-8&wd=fastapi參數(shù)匯總
其中,?后面的設(shè)定了參數(shù)ie為"UTF-8",參數(shù)wd為"fastapi"
這種參數(shù)的定義是所有參數(shù)中最簡單的,直接在函數(shù)定義中定義參數(shù)即可:
from fastapi import FastAPI
app=FastAPI()
app.get("/echo2/") #注意這里url上沒有定義參數(shù)
async def getEchoApi(text:str): #fastapi會(huì)聰明的發(fā)現(xiàn)它不是URL參數(shù),然后自動(dòng)將他識(shí)別為param參數(shù).
return {"echo":text}
運(yùn)行后:
localhost:8000/echo2?text=HelloWorld
返回{"echo":text}
3. body參數(shù)
這種參數(shù)主要常見于數(shù)據(jù)的提交等操作中,注意get操作是不支持?jǐn)y帶請(qǐng)求主體的,可以使用諸如POST,PUT等請(qǐng)求方式.
對(duì)于這種參數(shù)的調(diào)試,我們可以使用POSTMAN進(jìn)行.
from fastapi import FastAPI
from pydantic import BaseModel #fastapi的一個(gè)依賴,需要從pydantic中引入
app=FastAPI()
class EchoArgs(BaseModel): #繼承BaseModel
text:str #定義一個(gè)字符串型參數(shù)
app.post("/echo/") #get不支持請(qǐng)求體,更換請(qǐng)求方法
async def postEchoApi(args:EchoArgs): #設(shè)置剛才定義的參數(shù)
return {"echo":item.text} #原樣返回
返回同之前的一樣
4. form參數(shù)
這種參數(shù)主要常見于HTML中的表單,對(duì)于表單瀏覽器有額外的另一套規(guī)則,所以我們?cè)谶@里要定義其參數(shù)類型為表單參數(shù).
from fastapi import FastAPI,Form #引入Form用于定義表單參數(shù)
app=FastAPI()
app.post("/echo/")
async def postEchoApi(text:str=Form(None)): #通過Form設(shè)置參數(shù)
return {"echo":text} #原樣返回
效果同之前的一樣
5. cookie參數(shù)
cookie參數(shù),一般用于用戶的標(biāo)識(shí),用戶習(xí)慣的記錄等.各大網(wǎng)站都在使用這種方式,后來的session和token也要用到cookie技術(shù).cookie本質(zhì)是一種在客戶端保存的數(shù)據(jù),每次請(qǐng)求都會(huì)攜帶,你也可以在響應(yīng)中進(jìn)行設(shè)置.
from fastapi import FastAPI,Cookie
app=FastAPI()
app.post("/echo/")
async def postEchoApi(text:str=Cookie(None)):
return {"echo":text} #原樣返回
可以發(fā)現(xiàn),和form參數(shù)的定義十分相似.
6. file參數(shù)
file參數(shù)用于接收客戶端上傳的文件,有兩種方式,
from fastapi import FastAPI,File,UploadFile #引入文件相關(guān)操作
app=FastAPI()
app.post("/upload/2")
async def postUploadFile1Api(file:bytes=File(None)):
... #文件相關(guān)操作
app.post("/upload/1")
async def postUploadFile2Api(file:UploadFile=File(None)):
... #文件相關(guān)操作
其中,上傳的文件,可以使用bytes或UploadFile兩種格式,但我們推薦使用UploadFile方式,因?yàn)?
Using
UploadFile
has several advantages overbytes
:
- It uses a "spooled" file:
- A file stored in memory up to a maximum size limit, and after passing this limit it will be stored in disk.
- This means that it will work well for large files like images, videos, large binaries, etc. without consuming all the memory.
- You can get metadata from the uploaded file.
- It has a file-like
async
interface.
來自,fastapi官網(wǎng).
大意:
- 它使用“假脫機(jī)”文件:
- 存儲(chǔ)在內(nèi)存中的文件大小上限為最大毛萌,超過此限制后吭露,文件將存儲(chǔ)在磁盤中。
- 這意味著它可以很好地用于大型文件抢肛,例如圖像,視頻矫膨,大型二進(jìn)制文件等叠穆,而不會(huì)占用所有內(nèi)存。
- 您可以從上傳的文件中獲取元數(shù)據(jù)尸疆。
- 它具有類似類似文件的接口椿猎。
所以推薦使用UploadFile
以上是這篇文章的全部,錯(cuò)誤的地方懇請(qǐng)指正.
希望能和大家一起學(xué)習(xí).
最后,都看到這了,贊賞一下唄!(^ ~ ^||).