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

uploading flie to FastAPI endpoint using curl - 307 Temporary Redirect

我有一个 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 的文档界面上传文件时,这按预期工作 我能够 select 一个文件并成功上传。

但是,尝试使用 curl 进行相同操作失败:

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

curl 命令 returns 什么都没有,在服务器端记录了 307 Temporary Redirect

我不确定我在这里遗漏了什么

在某些 scenario/setup 中 FastAPI 重定向请求,使用带有完整 dns/ip 地址的 curl。

像这样:

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

或者也可以根据构建的应用添加headers(-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'