You can use url.Values’s Encode method. You could also use URL.String to build up the whole URL.
Client:
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
req, err := http.NewRequest("GET", "http://api.themoviedb.org/3/tv/popular", nil)
if err != nil {
log.Print(err)
os.Exit(1)
}
q := req.URL.Query()
q.Add("api_key", "key_from_environment_or_flag")
q.Add("another_thing", "foo & bar")
req.URL.RawQuery = q.Encode()
fmt.Println(req.URL.String())
// Output:
// http://api.themoviedb.org/3/tv/popular?another_thing=foo+%26+bar&api_key=key_from_environment_or_flag
var resp *http.Response
resp, err = http.DefaultClient.Do(req)
if err != nil {
log.Print(err)
}
defer resp.Body.Close()
}
Server:
r.ParseForm()
isDir := valuesGetDefault(r.Form, "isDir", "false")
func valuesGetDefault(values url.Values, key, defaultValue string) string {
v := values.Get(key)
if v == "" {
return defaultValue
} else {
return v
}
}