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!

6
  • 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. Commented Nov 18, 2016 at 17:00
  • @reticentroot yeah, my strucs are correct but where do I use them? how can I send them to the POST request? Commented Nov 18, 2016 at 18:04
  • Check out this stack, the accepted answer stackoverflow.com/questions/24455147/… it shows you how to make a post, you just might have to marshal your data using the json package. Commented Nov 18, 2016 at 18:16
  • @reticentroot but can I json encode a file? that's my issue Commented Nov 18, 2016 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. Commented Nov 18, 2016 at 18:32

1 Answer 1

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)

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.