Add a cookie to HTTP post request

5,196 views
Skip to first unread message

kamac

unread,
Aug 12, 2013, 5:30:05 PM8/12/13
to golan...@googlegroups.com
Hey there.
I am enjoying Golang so far, but it is not too easy to fully understand. (Lack of examples / tutorials :/)

Can anybody provide me with an example, of how would I go about adding a cookie to my Post request?

For example, I have this function:

func postExample() {
        values := make(url.Values)
        values.Set("some_var", "var1")
        r, err := http.PostForm("http://localhost/proj/test.php", values)
        if err != nil {
            fmt.Printf("error posting: %s", err)
            return
        }
        body, _ := ioutil.ReadAll(r.Body)
        r.Body.Close()
        fmt.Printf("stathat post result body: %s", body)
}

Any ideas?

Cheers 

Kyle Lemons

unread,
Aug 12, 2013, 7:57:45 PM8/12/13
to kamac, golang-nuts
Have you looked at net/http/cookiejar?


One option is to create a cookie jar, create an http.Client with it, and use that to make your post requests.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nigel Tao

unread,
Aug 12, 2013, 8:28:02 PM8/12/13
to kamac, golang-nuts
On Tue, Aug 13, 2013 at 7:30 AM, kamac <kama...@gmail.com> wrote:
> Can anybody provide me with an example, of how would I go about adding a
> cookie to my Post request?

Change

r, err := http.PostForm(etc)

to

c := &http.Client{Jar: jar}
r, err := c.PostForm(etc)

You will have to provide the jar variable that implements the
http.CookieJar interface. One option is to use the implementation
net/http/cookiejar package, if you're getting your cookie from an HTTP
request. You could also just hard-code the relevant cookies with a
one-off CookieJar implementation.

kamac

unread,
Aug 13, 2013, 2:04:55 AM8/13/13
to golan...@googlegroups.com
Okay, I'll try that too, but have you got any idea why this won't work? (I went with other advice that I got at golang-dev, where I accidentally posted this thread before)

client := &http.Client{};
req, _ := http.NewRequest("POST","http://localhost:8080/test/index.php",strings.NewReader("somevar=a"));
cookie := http.Cookie{"something","a","/",url,expire,expire.Format(time.UnixDate),86400,true,true,"something=a",[]string{"something=a"}};
req.Form.Add("somevar","HELLO");
resp, _ := client.Do(req);
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
fmt.Println(string(body[:len(body)]));

My cookie works here, but POST variable does not (it's like it isn't being sent) 

Kyle Lemons

unread,
Aug 13, 2013, 2:15:40 AM8/13/13
to kamac, golang-nuts
What are you doing with that cookie?  That doesn't look like it would compile (unused variable "cookie") -- you have to store it in the client's cookie jar. 

kamac

unread,
Aug 13, 2013, 3:01:41 AM8/13/13
to golan...@googlegroups.com, kamac
Ow, I forgot one line (which I have in my code):

req.AddCookie(&cookie);

kamac

unread,
Aug 13, 2013, 4:47:35 AM8/13/13
to golan...@googlegroups.com, kamac
Somehow,
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
Fixed it.

Cheers
Reply all
Reply to author
Forward
0 new messages