我有一個接收檔案并將其保存到磁盤的 fastAPI 端點,如下所示:
from fastapi import FastAPI, File, UploadFile
import shutil
app = FastAPI()
@app.post('/upload')
async def upload_file(file: UploadFile=File(...)):
with open(file.filename, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return {
"filename": file.filename,
}
當我通過檔案界面上傳檔案時,這按預期作業,http://localhost:8000/docs
我能夠選擇一個檔案并成功上傳。
但是,嘗試使用 curl 失敗:
curl -X POST localhost:8000/upload -F [email protected]
curl 命令不回傳任何內容,并且在服務器端307 Temporary Redirect記錄了 a 。
我不確定我在這里缺少什么
uj5u.com熱心網友回復:
在FastAPI重定向請求的某些場景/設定中,使用帶有完整 dns/ip 地址的 curl。
像這樣:
curl -X 'POST' '127.0.0.1:8000/upload' -F '[email protected]
或者也可以根據構建的應用程式添加標頭(-H)。
curl -X 'POST' \
'http://127.0.0.1:8000/upload' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F '[email protected];type=application/json'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321184.html
