用于下载 zip 文件的 swagger 规范的正确媒体类型

问题描述

我正在创建一个允许下载 zip 文件的 api,因此为此我正在寻找正确的媒体类型,以便在 swagger“2.0”规范中发送此 zip 文件作为响应。

我当前的 api 规范是这样的

/config:
  get:
    produces:
      - application/zip
    responses:
        200: # OK
          description: All config files
          schema:
            type: string
            format: binary

我已经用“go-swagger”编译了这个规范并为此实现了后端,但是当尝试调用这个 API 时我得到这个错误

http: panic serving 127.0.0.1:20366: applicationZip producer has not yet been implemented

在 swagger 的文档中我没有看到这种媒体类型

Official swagger media types

那么,如果我们想提供一个 API 来下载 zip 文件,那么正确的媒体类型应该是什么。 提前致谢

解决方法

您需要实现这一点。

由于以下生成的代码而发生错误:

func NewSampleAPI(spec *loads.Document) *SampleAPI {
    return &SampleAPI{
    ...
        ApplicationZipProducer: runtime.ProducerFunc(func(w io.Writer,data interface{}) error {
            return errors.NotImplemented("applicationZip producer has not yet been implemented")
        }),

所以在调用 NewSampleAPI 之后,你应该设置 ApplicationZipProducer

api := operations.NewSampleAPI(swaggerSpec)
api.ApplicationZipProducer = func(w io.Writer,data interface{}) error {
    ...
}
,

您应该使用“application/octet-stream”来实现以附件形式下载文件的 api,并且由于其 Producer 已经默认实现,因此您不会遇到此问题。