【问题标题】:Retrieve form data with python使用python检索表单数据
【发布时间】:2020-10-31 17:12:00
【问题描述】:

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

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

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

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

请帮助我应该如何编写 post_test 函数! (Flask 也可以。)

【问题讨论】:

标签: python curl fastapi


【解决方案1】:

您可以从我的 SO 答案中查看类似问题的完整答案 (How to send file to fastapi endpoint using postman)

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

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}

【讨论】:

  • 非常感谢。正是我想要的。
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 2015-03-18
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 2015-02-07
相关资源
最近更新 更多