How to fill HTTP request post form info?

8,391 views
Skip to first unread message

Andy W. Song

unread,
Apr 6, 2012, 10:53:26 PM4/6/12
to golang-nuts
Sorry for asking this maybe silly question:
Please see below snippet. Basically I want to send a POST but don't know how to fill the form. According to http://weekly.golang.org/pkg/exp/html/ http client will ignore request.Form and use Body. Then how do I put my form info into Body?

    conn, err := net.Dial("tcp", "someURL:80")
    if err != nil {
        panic(err)
    }
    c := httputil.NewClientConn(conn, nil)
    r, _ := http.NewRequest("POST", "http://someURL/post.php", nil)
    // Need to fill the form info here
    r, _ := c.Do(r)

Thanks
Andy

--
---------------------------------------------------------------
有志者,事竟成,破釜沉舟,百二秦关终属楚
苦心人,天不负,卧薪尝胆,三千越甲可吞吴

Jarod Liu

unread,
Apr 6, 2012, 11:02:15 PM4/6/12
to golan...@googlegroups.com
example from http package doc http://golang.org/pkg/net/http/
resp, err := http.PostForm("http://example.com/form",
	url.Values{"key": {"Value"}, "id": {"123"}})

Andy W. Song

unread,
Apr 6, 2012, 11:11:03 PM4/6/12
to Jarod Liu, golan...@googlegroups.com
I was intended to use low level httputil.ClientConn.Write() so that I can have more control over the behavior. So my question is how to manually compose the request body.

Thanks
Andy
Message has been deleted

Andy W. Song

unread,
Apr 6, 2012, 11:19:41 PM4/6/12
to Jarod Liu, golan...@googlegroups.com
Documents says the client will ignore this field. 


    // Form contains the parsed form data, including both the URL
    // field's query parameters and the POST or PUT form data.
    // This field is only available after ParseForm is called.
    // The HTTP client ignores Form and uses Body instead.
    Form url.Values


On Sat, Apr 7, 2012 at 11:15 AM, Jarod Liu <liuyu...@gmail.com> wrote:
http://golang.org/pkg/net/http/#Request
there is a Form field in struct Request   
r, _ := http.NewRequest("POST", "http://someURL/post.php", nil)
r.Form.Add("key", "value")

Jarod Liu

unread,
Apr 6, 2012, 11:22:44 PM4/6/12
to golan...@googlegroups.com, Jarod Liu
Yes, sorry. just read the source of func PostForm. That's the way to do it.
Reply all
Reply to author
Forward
0 new messages