I am building a URL with the front of the url
var SearchUrl = "https://www.example.org/3/search/movie?query="
Then form data which holds the keywords to search for.
var MovieSearch []string = r.Form["GetSearchKey"]
And the third part of the url which holds the api key
var apiKey = "&api_key=######"
I am using the ArrayToString()
to parse the form input data
func ArrayToString(array []string) string{
str := strings.Join(array, "+")
return str
}
And then building the url like this,
var SearchUrl = "https://api.example.org/3/search/movie?query="
var MovieSearch []string = r.Form["GetSearchKey"]
var apiKey = "&api_key=########"
UrlBuild := []string {SearchUrl, ArrayToString(MovieSearch), apiKey}
OUTPUT_STRING := ArrayToString(UrlBuild)
The output of the URL with one word works perfectly. The URL looks like this,
https://api.example.org/3/search/movie?query=+bad+&api_key=#####
When I add a second word to the input field the url looks like this,
https://api.example.org/3/search/movie?query=bad santa&api_key=e######
I need the keywords to not have a space between them like in the url that does not work.
This is my current attempt based on the answer from Cedmundo
func ArrayToQuery(values []string) string {
return url.QueryEscape(strings.Join(values, " "))
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
display(w, "search", &Page{Title: "Search"})
fmt.Println("method:", r.Method)
r.ParseForm()
var MovieSearch = r.Form["GetSearchKey"]
var SearchKeys = ArrayToQuery(MovieSearch)
params := fmt.Sprintf("?query=%s&api_key=eagaggagagagagagag", url.QueryEscape(SearchKeys))
perform := url.URL{
Scheme: "https",
Host: "api.example.org",
Path: "3/search/movie",
RawQuery: params,
}
fmt.Println(perform) // <- Calls .String()
}
Error
./main.go:63: url.QueryEscape undefined (type string has no field or method QueryEscape)
./main.go:75: url.QueryEscape undefined (type string has no field or method QueryEscape)
./main.go:76: url.URL undefined (type string has no field or method URL)
./main.go:94: undefined: OUTPUT_STRING
./main.go:96: undefined: OUTPUT_STRING