How to use go-swagger to define an attachment download











up vote
2
down vote

favorite












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 "rn" 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 rn


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









share|improve this question
























  • Please show us some code and what you've tried so far.
    – Phil
    Sep 7 '17 at 7: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 8 '17 at 7: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 8:08






  • 1




    Anyone get this working? I too am interested....
    – Dave Pascua
    Jan 16 at 23:01















up vote
2
down vote

favorite












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 "rn" 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 rn


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









share|improve this question
























  • Please show us some code and what you've tried so far.
    – Phil
    Sep 7 '17 at 7: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 8 '17 at 7: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 8:08






  • 1




    Anyone get this working? I too am interested....
    – Dave Pascua
    Jan 16 at 23:01













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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 "rn" 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 rn


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









share|improve this question















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 "rn" 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 rn


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






string file download attachment go-swagger






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 7 '17 at 8:41

























asked Sep 7 '17 at 6:56









EdS

112




112












  • Please show us some code and what you've tried so far.
    – Phil
    Sep 7 '17 at 7: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 8 '17 at 7: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 8:08






  • 1




    Anyone get this working? I too am interested....
    – Dave Pascua
    Jan 16 at 23:01


















  • Please show us some code and what you've tried so far.
    – Phil
    Sep 7 '17 at 7: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 8 '17 at 7: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 8:08






  • 1




    Anyone get this working? I too am interested....
    – Dave Pascua
    Jan 16 at 23:01
















Please show us some code and what you've tried so far.
– Phil
Sep 7 '17 at 7:04




Please show us some code and what you've tried so far.
– Phil
Sep 7 '17 at 7: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 8 '17 at 7:20






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 8 '17 at 7: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 8:08




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 8:08




1




1




Anyone get this working? I too am interested....
– Dave Pascua
Jan 16 at 23:01




Anyone get this working? I too am interested....
– Dave Pascua
Jan 16 at 23:01












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46089789%2fhow-to-use-go-swagger-to-define-an-attachment-download%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 3:23









        Nikhil Vandanapu

        147311




        147311






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46089789%2fhow-to-use-go-swagger-to-define-an-attachment-download%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?