0

I am currently working in Golang, I am developing an API, in a POST handler I need to receive in the Post form an array but with named positions, I mean, something like this:

myarray[a]:"some Value"
myarray[otherName]: "some Other value"
myarray[x] : "something different"

Right now I am trying making the Post request using curl in the CLI. I am sending this:

curl -i -X POST --url http://localhost:20000/myendpoint -H "Content-Type: application/x-www-form-urlencoded" -d 'Name=Comp&myarray[x]=somethingdifferent&myarray[otherName]=someOtherValue'

And, indeed when I print the form values in Go, I get:

[myarray[x]:[somethingdifferent] myarray[otherName]:[someOtherValue]]

Until here I understand everything, then I need to get the array myarray in a golang variable, how can I do this? When I do:

req.Form["myarray"]

I get nothing there, my purpose is to get that array and store it as a JSON object in the database due that I don't know which field can be sent in that array. I need something like:

myarray[[x]=somethingdifferent,[otherName]=someOtherValue]
6
  • 1
    That's not HTTP query parameters work, you have 2 separate variables called "myarray[x]" and "myarray[otherName]". Are you trying to parse those into a map? Commented Oct 6, 2017 at 15:02
  • @JimB correct, right now I am getting them as two separate variables, but I need them in some way in one array so then I can convert it to json and save it Commented Oct 6, 2017 at 15:06
  • 1
    There's no shortcut here, you need to parse those strings and assign them to a map. A json array can't have string indices, so you can use a slice (and you don't want an array in Go). Commented Oct 6, 2017 at 15:20
  • Seems like there's a general confusion in this question about the difference between an array and a map/hash/dictionary. Commented Oct 6, 2017 at 15:29
  • Sorry to add to the confusion, that should be "you can't use a slice". Commented Oct 6, 2017 at 15:35

1 Answer 1

2

You may parse it yourself:

func extractDynArray(form url.Values, key string) (result map[string]interface{}, err error) {
    result = make(map[string]interface{})
    reg, err := regexp.Compile(`^([a-z]+)\[([a-z]+)\]$`)
    if err != nil {
        log.Fatalf("Error compiling regexp: %v", err)
    }
    var matches [][]string
    for k, v := range form {
        matches = reg.FindAllStringSubmatch(k, -1)
        if len(matches) != 1 {
            continue
        }
        if key != "" && matches[0][1] != key {
            continue
        }
        if len(matches[0]) != 3 {
            continue
        }
        result[matches[0][2]] = v
    }
    return
}

func handler(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        log.Fatalf("Cannot parse form %v", err)
    }
    //form := r.Form["myarray"]
    fmt.Printf("Form: %+v\n", r.Form)
    fmt.Printf("Form myarray: %+v \n", r.Form["myarray"])
    parsed, err := extractDynArray(r.Form, "myarray")
    fmt.Printf("Parsed: %v. Err: %v", parsed, err)
}
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.