如何指定在swagger中返回pdf?

Sco*_*ham 19 swagger swagger-ui swagger-2.0 swagger-editor

我在我的swagger REST API中有一个需要返回pdf文件的调用.没有明确的示例/文档说明如何在不导致语法错误的情况下执行此操作.

  responses:
    200:
      description: Returns PDF
      schema: [application/pdf]
Run Code Online (Sandbox Code Playgroud)

  responses:
    200:
      description: Returns PDF
      schema: 
        type: file
Run Code Online (Sandbox Code Playgroud)

  responses:
    200:
      description: Returns PDF
      schema:
        type:  [application/pdf]
Run Code Online (Sandbox Code Playgroud)

都失败了.这甚至可能吗?

dev*_*918 23

使用 Open API 3.0,尝试以下响应:

get:
  summary: Returns the report in the PDF format
  responses:
    '200':
      description: A PDF file
      content:
        application/pdf:
          schema:
            type: string
            format: binary
Run Code Online (Sandbox Code Playgroud)

文档:swagger.io/docs/specification/describing-responses

部分:返回文件的响应


Ron*_*Ron 21

  responses:
    200:
      description: Returns PDF
      schema: 
        type: file
Run Code Online (Sandbox Code Playgroud)

在你给出的选项中,这是正确的选择.另外,请务必使用produces: [application/pdf]

如果它失败了,请确保使用最新版本的编辑器.有一个与file最近解决的类型相关的错误.

  • 使用 Open API 3.0 ,请尝试以下 `responses: '200': description: A PDF file content: application/pdf: schema: type: string format: bin` 文档:https://swagger.io/docs/specification/describing-响应/部分:返回文件的响应 (2认同)
  • @dev1918你应该将其发布为另一个答案。 (2认同)