使用OpenAPI接收多部分邮件中的文件

时间:2019-09-24 03:23:46

标签: python api yaml openapi

我有一个在Flask / Connexion上运行的API。该API是用Swagger的OpenAPI 3编写的。我通过multipart / form-data将数据发送到端点,并且所有内容都通过除了上载的文件获得。我将从共享我的OpenAPI YAML文件开始。对于文件,我参考了this document关于如何指定架构的信息。

openapi: "3.0.0"

info:
  description: No one is reading this
  version: "1.0.0"
  title: Super basic API endpoint

# Paths supported by the server application
paths:
   /api/submit:
    post:
      summary: Process a new text submission asynchronously
      operationId: submit.submit
      requestBody:
        content:
          multipart/form-data:
            schema:
              x-body-name: submission
              $ref: '#/components/schemas/Submission'
      responses:
        '200':
          description: That's nice

components:
  schemas:
    Submission:
      type: object
      required:
        - url
        - mimeType
        - language
        - content
      properties:
        url:
          type: string
          format: uri
        mimeType:
          type: string
          description: MIME type of the file submission
        language:
          type: string
          pattern: '^[a-z]{3}$'
        content:
          type: array
          items:
            type: string
            format: binary

submit.submit 操作调用以下Python代码:

from datetime import datetime
import uuid

def get_timestamp():
    return datetime.now().strftime(("%Y-%m-%d %H:%M:%S"))

def submit(submission):
    print(str(submission))

    return { "timestamp": get_timestamp(), "uuid": str(uuid.uuid4()) }

Python打印的内容是

{'url': 'http://nosite.none', 'mimeType': 'text/plain', 'language': 'eng'}

因此不存在“内容”。但是我知道数据已经发送,因为我是在浏览器中将其捕获到的。

-----------------------------138508216052
Content-Disposition: form-data; name="url"

http://nosite.none
-----------------------------138508216052
Content-Disposition: form-data; name="mimeType"

text/plain
-----------------------------138508216052
Content-Disposition: form-data; name="language"

eng
-----------------------------138508216052
Content-Disposition: form-data; name="content"; filename="nonsense.txt"
Content-Type: text/plain

Now is the time for
all good men to come
to the aid of their
party.
-----------------------------138508216052--

我应该对Connexion进行其他调试,以告诉我是否/为什么不传递“内容”文件吗?

0 个答案:

没有答案