使用python检索表单数据



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

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

现在,在服务器端,我想检索图像,并将每个像素值加1,然后返回它;捕获";卷曲的图像,我该怎么做?现在,我只有下面的伪函数,它不会做任何智能的事情:

@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答案中查看类似问题的完整答案(如何使用邮递员将文件发送到fastapi端点(

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

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}

最新更新