I am creating a front url url
var SearchUrl = "https://www.example.org/3/search/movie?query="
Then create data that contains the keywords to search.
var MovieSearch []string = r.Form["GetSearchKey"]
And the third part of the url that contains the api key
var apiKey = "&api_key=######"
I use ArrayToString()to analyze the input of the form
func ArrayToString(array []string) string{
str := strings.Join(array, "+")
return str
}
And then by building such a URL,
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 result of a single-word URL works fine. The url is as follows:
https://api.example.org/3/search/movie?query=+bad+&api_key=
When I add the second word in the input field, the url looks like this:
https://api.example.org/3/search/movie?query=bad santa&api_key=e
I need keywords so that there is no space between them, as in a URL that does not work.
This is my current attempt based on a response 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)
}
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