snippetgoCritical
How to send a POST request in Go?
Viewed 0 times
sendpostrequesthow
Problem
I am trying to make a POST request but I can't get it done. Nothing is received on the other side.
Is this how it is supposed to work? I'm aware of the
Is this how it is supposed to work? I'm aware of the
PostForm function but I think I can't use it because it can't be tested with httputil, right?hc := http.Client{}
req, err := http.NewRequest("POST", APIURL, nil)
form := url.Values{}
form.Add("ln", c.ln)
form.Add("ip", c.ip)
form.Add("ua", c.ua)
req.PostForm = form
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
glog.Info("form was %v", form)
resp, err := hc.Do(req)Solution
You have mostly the right idea, it's just the sending of the form that is wrong. The form belongs in the body of the request.
req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))Code Snippets
req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))Context
Stack Overflow Q#24493116, score: 178
Revisions (0)
No revisions yet.