24

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

  responses:
    200:
      description: Returns PDF
      schema: [application/pdf]

  responses:
    200:
      description: Returns PDF
      schema: 
        type: file

  responses:
    200:
      description: Returns PDF
      schema:
        type:  [application/pdf]

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

4

2 回答 2

22
  responses:
    200:
      description: Returns PDF
      schema: 
        type: file

在您提供的选项中,这是正确的选项。另外,请务必使用produces: [application/pdf]

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

于 2015-08-25T06:42:29.900 回答
3

使用 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

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

部分:返回文件的响应

于 2021-10-17T20:32:49.303 回答