I create my form like this:
form := url.Values{}
form.Add("region", "San Francisco")
if len(params) > 0 {
for i := 0; i < len(params); i += 2 {
form.Add(params[i], params[i+1])
}
testLog.Infof("form %v", form)
Now if I use
resp, err = http.PostForm(address+r.Path, form)
then everything works fine, I get back a response with an expected cookie.
However, I would like to to add a header in which case I can't use PostForm
hence I created my POST request
manually like:
req, err := http.NewRequest("POST", address+r.Path, strings.NewReader(form.Encode()))
Then I add stuff to the header and send the request
req.Header.Add("region", "San Francisco")
resp, err = http.DefaultClient.Do(req)
But the form is not received and my response does not contain any cookie.
When I print the req
, it looks like the form is nil
:
&{POST http://localhost:8081/login HTTP/1.1 1 1 map[Region:[San Francisco]] {0xc420553600} 78 [] false localhost:8081 map[] map[] <nil> map[] <nil> <nil> <nil> <nil>}