curl POST 上传文件测试

Curl POST upload file test

我有一个具有以下类型的 FastAPI:

@app.post("/extract_text") 
async def create_upload_file(upload_file: UploadFile = File(...)):
    return FileResponse(path="Outputs/ocr_output.zip", filename="{}".format(main.allinall(upload_file))+"_output.zip", media_type='application/zip')

在浏览器中,借助我拥有的 UI,我能够上传文件并下载输出文件 (return...)。

我想在 linux 服务上测试这个,我必须通过 curl 命令测试这个 api。我正在使用这个命令:

 curl -X 'POST' 'http://localhost/extract_text' -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -F 'file=@demo2_test_imageinput.png;type=application/json'

这是我收到的错误:

{"detail":[{"loc":["body","upload_file"],"msg":"field required","type":"value_error.missing"}]}

输入是当前文件夹中可用的 png 文件,输出将是 zip 文件作为 allinall 函数的输出。

当我尝试这个时:

curl -i -X POST -H "Content-Type: multipart/form-data" -F "data=@demo2_test_imageinput.png" http://localhost/extract_text

我收到此错误:

HTTP/1.1 422 Unprocessable Entity
Server: nginx/1.15.12
Date: Thu, 19 May 2022 20:16:44 GMT
Content-Type: application/json
Content-Length: 95
Connection: keep-alive

这是你弄错的地方。

file=@demo2_test_imageinput.png

根据您的函数定义,您的多部分字段名称应为 upload_file

async def create_upload_file(upload_file: UploadFile = File(...))

所以你的curl请求必须有参数

upload_file=@demo2_test_imageinput.png