如何在Swagger中指定返回PDF文件?

32

我的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]

全部失败。这真的可能吗?

2个回答

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

在你给出的选项中,那个是正确的选项。还要确保使用produces:[application/pdf]

如果它对你失败了,请确保使用最新版本的编辑器。最近已经解决了与file类型有关的错误。


5
使用 Open API 3.0,可尝试以下代码:responses: '200': description: PDF 文件 content: application/pdf: schema: type: string format: binary文档链接:https://swagger.io/docs/specification/describing-responses/ 章节:返回文件的响应 - nitashathakur
2
@dev1918,你应该将它发布为另一个答案。 - Innokenty

22

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

部分:返回文件的响应


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,