1

I'm already posting a file and some params, but now I need to send nested params with a struct and I don't know where to use that (I'm new in Go).

This is what I have: https://play.golang.org/p/L4qx6AZhUO

Now, I'm creating this structs:

type S3 struct {
    Accesskeyid     string
    Secretaccesskey string
    Bucket          string
}

type Test struct {
    Outputformat string
    Wait         string
    Output       S3
    File         []byte
}

And I would like to send the Test struct WITH the file. Any ideas?

Thanks!

Mati Tucci
  • 2,826
  • 5
  • 28
  • 40
  • Can you be a bit more specific? Looking at your code I see nothing wrong with your structs. Output is a type of S3, so you've already created a nested object.. And if the file is converted into a byte array then the field File should have no problem accepting it. So I guess can you give more details about the issue, Error messages, etc. – reticentroot Nov 18 '16 at 17:00
  • @reticentroot yeah, my strucs are correct but where do I use them? how can I send them to the POST request? – Mati Tucci Nov 18 '16 at 18:04
  • Check out this stack, the accepted answer http://stackoverflow.com/questions/24455147/how-do-i-send-a-json-string-in-a-post-request-in-go it shows you how to make a post, you just might have to marshal your data using the json package. – reticentroot Nov 18 '16 at 18:16
  • @reticentroot but can I json encode a file? that's my issue – Mati Tucci Nov 18 '16 at 18:30
  • File is just the name you gave it, it's of type byte array, so yes. Json should be able to Marshal the struct and all its data. – reticentroot Nov 18 '16 at 18:32
  • @reticentroot I tried but didn't work. Can you answer me with the code, please? And will upvote it. – Mati Tucci Nov 18 '16 at 18:43

1 Answers1

0

Okay, So given what you've told me and what little information I have with your example, I might do something like this.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

type S3 struct {
    Accesskeyid     string
    Secretaccesskey string
    Bucket          string
}

type Test struct {
    Outputformat string
    Wait         string
    Output       S3
    File         []byte
}

func main() {
    myStrut := Test{
        Outputformat: "Json",
        Wait:         "Some Time",
        Output: S3{
            Accesskeyid:     "my id",
            Secretaccesskey: "secret id",
            Bucket:          "east",
        },
        File: []byte(`some bytes`),
    }
    jsonValue, err := json.Marshal(myStrut)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Test showing that data was marshalled\n %q\n", jsonValue)
    resp, err := http.Post("some url", "application/json", bytes.NewBuffer(jsonValue))
    if err != nil {
        panic(err)
    }
    fmt.Println(resp.Status)

}

Now from what i gleamed in the comments you might be also having trouble opening the file as a byte array to assign to your struct. Here's an example you can use you help you open the file as a byte array so that you can assign those bytes to your struct.

package main

import (
    "fmt"
    "io/ioutil"
)

func main() {

    //An example of how to open a file and turn it into bytes for your struct
    byteArray, err := ioutil.ReadFile("input.txt")
    if err != nil {
        panic(err)
    }
    fmt.Println(byteArray)

}
reticentroot
  • 3,612
  • 2
  • 22
  • 39