使用 python 检索表单数据

Retrieve form data with python

我是网络通信的新手。 我使用 ubuntu 并尝试学习 fastapi。 假设我 post 使用 curl 的文件。似乎普遍认为这是最好的方法:

curl -F "file=@image1.jpg" http://127.0.0.1:8000/image -v

现在,在服务器端,我想检索图像并将每个像素值加 1,然后 return。但我现在知道如何从卷曲中“捕捉”图像,我该怎么做?现在,我只有下面的虚拟函数,它没有做任何智能操作:

@app.post("/image")
async def post_test():
    print("I don't know how to catch the image :( ")
    return {"You sent an image..."}

请帮助我如何编写 post_test 函数! (烧瓶也可以。)

您可以查看我对类似问题的 SO 回答中的完整答案 ()

基本上,您必须将代码更改为

from fastapi import FastAPI, UploadFile, File


app = FastAPI()


@app.post("/file/")
async def create_upload_file(file: UploadFile = File(...)):
    # Access your file object via file.file,
    # and perform all the necessary transformations
    # Return the filename, but you may return the file itself
    return {"filename": file.filename}