Thellimist
Thellimist

Reputation: 4007

How to get array in http request

For this request GET http://localhost:8080/path?my_key%5B%5D=3&my_key%5B%5D=4&my_key%5B%5D=5

I can't get the data from my_key. I tried req.URL.Query()["my_key"]. I can get it if I change the request encoding to from my_key%5B%5D=4&my_key%5B%5D=5 to my_key=4&my_key=5

How can I get request URL's in form of my_key[]=value

Upvotes: 1

Views: 3900

Answers (1)

user3591723
user3591723

Reputation: 1284

Use the net/url package

package main

import (
    "fmt"
    "net/url"
)

func main() {
    utmp := "http://localhost:8080/path?my_key%5B%5D=3&my_key%5B%5D=4&my_key%5B%5D=5"
    u, err := url.Parse(utmp)
    if err != nil {
        panic(err)
    }
    fmt.Println(u.Query()["my_key[]"])
}

https://play.golang.org/p/t2O7KnUbZOA

Your key is "my_key[]" not "my_key"

Upvotes: 3

Related Questions