从 openapi swagger 上传的文件在后端 python 函数中未收到

发布于 2025-01-09 22:34:02 字数 1154 浏览 0 评论 0原文

我正在尝试使用 Openapi 3.0 和 swagger UI 从用户那里获取文件。但是我没有在我的 python 函数中获取该文件进行处理。下面是我的代码:

code.py

def get_file():
    try:
        file=request.files.getlist('file')[0]
        with open(file, 'r') as fp:
            files = {"file": (file, fp)}
            response = requests.post(server, files=files)
            return response.json()
    except Exception as exc:
        return exc

api.yaml

  /get-result:
    post:
      summary: "A function to get file"
      operationId: "code.get_file"
      requestBody:
      content:
        application/json:
          schema:
            type: string
              format: binary
      responses:
        200:
          description: "executed successfully"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/myschema"
        500:
          description: Server is down.

我已经引用了此链接: 在 Swagger 中上传文件并在 Flask 后端接收 然而,这是针对 Openapi 2.0 的,并没有帮助,因为我使用的是 openapi 3.0

I am trying to take a file from user using Openapi 3.0 and swagger UI. However i am not getting that file for processing in my python function. Below is my code:

code.py

def get_file():
    try:
        file=request.files.getlist('file')[0]
        with open(file, 'r') as fp:
            files = {"file": (file, fp)}
            response = requests.post(server, files=files)
            return response.json()
    except Exception as exc:
        return exc

api.yaml

  /get-result:
    post:
      summary: "A function to get file"
      operationId: "code.get_file"
      requestBody:
      content:
        application/json:
          schema:
            type: string
              format: binary
      responses:
        200:
          description: "executed successfully"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/myschema"
        500:
          description: Server is down.

I have already referred this link: Upload a file in Swagger and receive at Flask backend
However this is for Openapi 2.0 and didnt help as I am using openapi 3.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2025-01-16 22:34:02

检查是否

request.files['file']

可以从请求中获取文件,我不确定该行是否

file=request.files.getlist('file')[0]

实际上会获取正确的文件(或仅获取列表?)

check if

request.files['file']

can get the file from the request, I'm not sure if the line

file=request.files.getlist('file')[0]

will actually get the correct file (or only a list?)

平安喜乐 2025-01-16 22:34:02

下面的代码更改帮助我解决了这个问题:

api.yaml

/get-result:
    post:
      summary: "A function to get file"
      operationId: "code.get_file"
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        200:
          description: "executed successfully"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/myschema"
        500:
          description: Server is down.

在下面的 code.py 中,必须将参数“file”传递给函数,这也是 openapi 规范中的字段名称。 发送文件指针和文件名也很重要

使用 post 请求code.py

def get_file(file):
    try:
        fp=file.read()
        file.save(file.filename)
        response = requests.post(server, files={"file":(file.filename,fp)})
        return response.json()
    except Exception as exc:
        return exc

Below code changes helped me to get this resolved:

api.yaml

/get-result:
    post:
      summary: "A function to get file"
      operationId: "code.get_file"
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        200:
          description: "executed successfully"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/myschema"
        500:
          description: Server is down.

In below code.py it was essential to pass the parameter 'file' to the function which is also the field name in openapi specification. Also its important to send file pointer and filename with the post request

code.py

def get_file(file):
    try:
        fp=file.read()
        file.save(file.filename)
        response = requests.post(server, files={"file":(file.filename,fp)})
        return response.json()
    except Exception as exc:
        return exc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文