String 如何使用go-swagger定义附件下载
我正在使用String 如何使用go-swagger定义附件下载,string,file,download,attachment,go-swagger,String,File,Download,Attachment,Go Swagger,我正在使用go-swagger下载附件。这些是小的多行文件,另一端只有一个浏览器。 我尝试将响应定义为'string',但无法用多行文本填充负载,它以“\r\n”而不是换行符到达。我还尝试了'string'格式'binary',但是客户端看到一个包含读取器{}的响应。我对200响应的内容yaml如下所示: headers: Content-Disposition: type: string pattern: attachment; file
go-swagger
下载附件。这些是小的多行文件,另一端只有一个浏览器。
我尝试将响应定义为'string'
,但无法用多行文本填充负载,它以“\r\n”而不是换行符到达。我还尝试了'string'
格式'binary'
,但是客户端看到一个包含读取器{}
的响应。我对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
编码响应。
有什么建议吗
这就是我迄今为止所尝试的:
正在尝试“字符串”格式“字节”
正在尝试“字符串”
正在尝试“字符串”格式“二进制”
为了重申对该问题的评论,任何人只要在go swagger中创建一个允许文件下载的端点,只需在该方法中添加一个
生成应用程序/octet流
。请向我们展示一些代码和您迄今为止的尝试。我找到了答案。我在yaml文件中使用了文本生成器,我需要为响应添加:products:-application/octet-stream到响应yaml并重新构建,奇怪的是,上面的内容适用于CURL客户机,但使用GO HTTP客户机仍然会返回我一个“Reader{}”——其中ioutil.ReadAll()给出一个空字符串。这似乎是一个限制?有人能做到吗?我也对……感兴趣。。。。
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{}