如何使用go-swagger定义附件下载

时间:2017-09-07 06:56:25

标签: string file download attachment go-swagger

我正在使用go-swagger下载附件。这些是小型多行文件,另一端只有一个浏览器。 我尝试将响应定义为'string',但是找不到用多行文本填充有效负载的方法,它来自" \ r \ n"而不是换行符。我还尝试了'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 \r\n

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

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{}

1 个答案:

答案 0 :(得分:0)

要重申对这个问题的评论,任何试图简单地创建端点以允许使用swsagger进行文件下载的人,只需在方法中添加produces application/octet-stream