如何创建一个 swagger:response 来生成二进制应用程序/pdf 文件?

问题描述 投票:0回答:3

我无法在 swagger、connexion、openapi3 中下载文件。在 openapi 规范中,我定义了以下路径:

/lab/samples/list/pdf:
get: 
  summary: download pdf file
  operationId: get_sample_pdf
  responses:
    "200":
        application/pdf:
          schema:
            type: string
            format: binary  
  security:
  - labuserAuth: []
  x-openapi-router-controller: swagger_server.controllers.lab_controller#

调用被转发到我的 lab_controler.py,simplay 返回二进制 pdf

def list_sample_pdf():
    f_pdf = "list_samples.pdf"
    with open(f_pdf, "rb") as f:
        return f.read()

调用端点时我收到

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xdf in position 10: invalid continuation byte

当我寻找答案时,我偶然发现了一堆不同的线程,但它们都不能真正给我提示我做错了什么

openapi端需要哪些配置?我的控制器实现应该返回什么,以便 connexion 能够正确处理 pdf?

这就是我运行应用程序的方式

    app = connexion.App(__name__, specification_dir='./swagger/')
    app.json_encoder = encoder.JSONEncoder
    app.add_api('swagger.yaml', arguments={'title': 'sample-submission and notification service'}, pythonic_params=True)

    #add CORS support to send Access-Control-Allow-Origin header
    CORS(app.app,expose_headers=["Content-Disposition: "])
    app.run(host="127.0.0.1", port=8083)

我还尝试添加应用程序/八位字节流响应

application/octet-stream:
                  encoding:
                    file:
                      headers:
                        Content-Disposition:
                          schema:
                            type: string
                            format: binary
                            example: attachment; filename="name.pdf"

当不是返回 pdf 文件,而是

with open(f_csv, "rb") as f: return f.read()
一个 UTF-8 可读文件作为这个简单的 csv 文件时,非二进制文件的内容由 connexion 作为 application/json 响应返回

python-3.x openapi swagger-codegen content-disposition connexion
3个回答
0
投票

您是否尝试过添加标头定义

paths:          
  /examples:
    get:
      responses:
        '200':
          description: ok
          content:
            application/pdf:
              schema:
                type: string
                format: binary
          headers:
            Content-Disposition:
              schema:
                type: string
                description: Used only with `application/pdf` responses
                example: attachment; filename="name.pdf"

0
投票

来自 https://swagger.io/docs/specification/describing-responses/

paths:
 get:
  summary: Returns the report in the PDF format
  responses:
    '200':
      description: A PDF file
      content:
        application/pdf:
          schema:
            type: string
            format: binary

-1
投票

作为一种不太优雅的解决方法,您可以执行以下操作来返回:

 import flask, os
    
    resp = flask.send_from_directory(os.path.abspath(os.getcwd()), 'file_to_send.pdf',
            as_attachment=True, 
            mimetype='application/pdf',
            attachment_filename='your_pdf_name.pdf'
        )

    return flask.Response(
            response = resp.response,
            status=200,
            content_type='application/pdf',
            headers=resp.headers,
            mimetype=resp.mimetype
        )
© www.soinside.com 2019 - 2024. All rights reserved.