我正在使用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编码的响应。对此有什么建议吗?
这就是我到目前为止所尝试的:
正在尝试"string“格式"byte"...
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{}发布于 2018-11-13 11:23:11
为了重申对这个问题的评论,任何试图简单地创建一个允许go-swagger中文件下载的端点的人,只需向该方法添加一个produces application/octet-stream即可。
https://stackoverflow.com/questions/46089789
复制相似问题