2

I am using go-swagger to download attachments. These are small multi-line files, and there is just a browser on the other end. I tried defining the response as 'string', but can find no way to populate the payload with multiline text, it arrives with "\r\n" instead of newlines. I also tried 'string' format 'binary', but then the client sees a response containing a Reader{}. My content yaml for the 200 response looks like this:

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

I also tried 'string' format 'byte', but I don't want a base64 encoded response. Any advice on this?

This is what I have tried so far:

trying "string" format "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

trying "string"

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

trying "string" format "binary"

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{}
EdS
  • 31
  • 3
  • Please show us some code and what you've tried so far. – Philip Sep 07 '17 at 07:04
  • I found the answer. I had used the text producer in the yaml file, I needed to make that into a octet-stream producer for this response by adding: produces: - application/octet-stream To the response yaml and re-building – EdS Sep 08 '17 at 07:20
  • Curiously, the above works for CURL client, but using a GO HTTP client still returns me a "Reader{}" - where ioutil.ReadAll() gives an empty string. This appears to be a limitation? – EdS Sep 13 '17 at 08:08
  • 1
    Anyone get this working? I too am interested.... – Dave Pascua Jan 16 '18 at 23:01

1 Answers1

2

To reiterate the comment on the question, anyone who's trying to simply create an endpoint which allows for file download in go-swagger, just add an produces application/octet-stream to the method.

Nikhil Vandanapu
  • 489
  • 10
  • 18