使用curl - 307临时重定向上传飞到FastAPI端点



我有一个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的docs界面上传文件时,这就像预期的那样工作了我可以选择一个文件并成功上传。

但是,对curl进行相同的尝试失败:

curl -X POST localhost:8000/upload -F file=@photo.png

curl命令不返回任何内容,并且在服务器端记录了一个307 Temporary Redirect

我不知道我在这里错过了什么

FastAPI重定向请求的某些场景/设置中,使用带有完整dns/ip地址的curl

像这样:

curl -X 'POST' '127.0.0.1:8000/upload' -F 'file=@photo.png

或者也可以根据构建的应用程序添加头文件(-H)。

curl -X 'POST' 
'http://127.0.0.1:8000/upload' 
-H 'accept: application/json' 
-H 'Content-Type: multipart/form-data' 
-F 'file=@photo.png;type=application/json'

最新更新