I am trying to make a Post request using in GO Lang. The API URL is expecting a username and password field to be passed to it in body. However I keep getting the below error ?
I am not sure what am i doing wrong here ?
Error
url.Values undefined (type string has no field or method Values)
Go Function
func makeHttpPostReq(url string, username string, password string){
client := http.Client{}
req, err := http.NewRequest("POST", url, url.Values{"username": {username}, "password": {password}})
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Unable to reach the server.")
} else {
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("body=", string(body))
}
}
The argument to makeHttpPostReq
states url is a string but you are treating it as a struct url.Values
, thus the error
has no field or method
You are reusing the url
word.
url is in your case resolving to url as a string not a net/url