HTTP Post request with auth header

5,323 views
Skip to first unread message

sty...@gmail.com

unread,
Aug 7, 2013, 5:27:47 PM8/7/13
to golan...@googlegroups.com
Hey guys,

I can't seem to figure out how to create a http post request with basic auth.

I figured out the following:

Create a new request with 
client := &http.Client{}

req := http.NewRequest("POST", "URL", nil)
req.SetBasicAuth('u', 'p')
client.Do(req)

Problem with this is I have no clue as to how to set the post data.

With the following example I don't know how to set the auth header:

client.PostForm("URL", url.Values{ ... values ...})

Help is much appreciated! :)

Cheers,

Jeff

Rodrigo Kochenburger

unread,
Aug 7, 2013, 6:44:44 PM8/7/13
to sty...@gmail.com, golan...@googlegroups.com
The 3rd parameter for http.NewRequest is the body.

- RK


--
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.
 
 

Christoph Hack

unread,
Aug 7, 2013, 6:46:35 PM8/7/13
to golan...@googlegroups.com, sty...@gmail.com
I think it's possible to simply use the PostForm method with a url like "http://user:pa...@example.com" (but I haven't tried it). Anyway, if you want to create a request object on your own, you can do so:

data := url.Values{ ... values ... }
req := http.NewRequest("POST", "URL", strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth('u', 'p')
client.Do(req)

Regards,
Christoph

Jeffrey Wilcke

unread,
Aug 7, 2013, 6:50:45 PM8/7/13
to Christoph Hack, golan...@googlegroups.com
Ahah! I see now. Thanks alot! Kinda start doing Go since todo as I finally managed to take some time and get through the "A Tour of Go".

Thanks again!

Rodrigo Kochenburger

unread,
Aug 7, 2013, 6:57:11 PM8/7/13
to Jeffrey Wilcke, Christoph Hack, golan...@googlegroups.com
Not a problem. And welcome to Go :-)

- RK


xfst...@gmail.com

unread,
May 21, 2017, 11:07:18 AM5/21/17
to golang-nuts, sty...@gmail.com
thank for you example

在 2013年8月8日星期四 UTC+8上午6:46:35,Christoph Hack写道:
Reply all
Reply to author
Forward
0 new messages