如何使用Go-Swagger定义附件下载



我正在使用 go-swagger下载附件。这些是小型多行文件,另一端只有一个浏览器。我尝试将响应定义为'string',但无法找到用多行文本填充有效载荷的方法,它以" r n"而不是newlines到达。我还尝试了'string'格式'binary',但随后客户看到包含Reader{}的响应。我的200个响应的内容yaml看起来像:

headers:
        Content-Disposition:
          type: string
          pattern: attachment; filename="attachement.txt"
        Content-Type:
          type: string
          pattern: application/octet-stream
      schema:
        type: string

我还尝试了'string'格式'byte',但我不想要base64编码的响应。对此有任何建议吗?

这是我到目前为止尝试的:

尝试"字符串"格式"字节" ...

  payload := bytes.NewBufferString("first line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    payload.WriteString("second line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    resp := responses.NewGetResponseInfoOK()
    resp.SetPayload(payload)
  // fails.. will not accept payload other than strfmt.Bas64

尝试"字符串"

payload := bytes.NewBufferString("first line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    payload.WriteString("second line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    resp := responses.NewGetResponseInfoOK()
    resp.SetPayload(payload.String())
  // accepts payload, but 13/10 get converted into rn

尝试"字符串"格式"二进制"

type nopCloser struct {
      io.Reader
  }
  func (nopCloser) Close() error { return nil }

  payload := bytes.NewBufferString("first line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    payload.WriteString("second line")
    payload.WriteByte(13)
    payload.WriteByte(10)
    resp := responses.NewGetResponseInfoOK()
    resp.SetPayload(nopCloser(payload))
  // accepts payload, but the browser sees a Reader{}

要重申有关问题的评论,任何试图创建一个端点的人,该端点允许在go-swagger中下载文件,只需将 produces application/octet-stream添加到方法中。

<</p>

相关内容

  • 没有找到相关文章

最新更新