From 9a583323a8a1e2956fcc357c11e0fa3fe481be25 Mon Sep 17 00:00:00 2001 From: smallfish Date: Wed, 20 Aug 2014 23:36:58 +0800 Subject: [PATCH 1/3] Add SetBasicAuth function for HTTP Auth --- httplib/httplib.go | 6 ++++++ httplib/httplib_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/httplib/httplib.go b/httplib/httplib.go index 1c0d5544..bb5fb9ea 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -147,6 +147,12 @@ func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest return b } +// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password. +func (b *BeegoHttpRequest) SetBasicAuth(username, password string) *BeegoHttpRequest { + b.req.SetBasicAuth(username, password) + return b +} + // SetEnableCookie sets enable/disable cookiejar func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { b.setting.EnableCookie = enable diff --git a/httplib/httplib_test.go b/httplib/httplib_test.go index 2d49d875..02068c0b 100644 --- a/httplib/httplib_test.go +++ b/httplib/httplib_test.go @@ -120,6 +120,18 @@ func TestWithCookie(t *testing.T) { } } +func TestWithBasicAuth(t *testing.T) { + str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String() + if err != nil { + t.Fatal(err) + } + t.Log(str) + n := strings.Index(str, "authenticated") + if n == -1 { + t.Fatal("authenticated not found in response") + } +} + func TestWithUserAgent(t *testing.T) { v := "beego" str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String() From e70537f8b3e6db63a7a6a9978acb2efb9863fe55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B0=8F=E7=8E=89?= Date: Thu, 21 Aug 2014 00:03:03 +0800 Subject: [PATCH 2/3] Update README --- httplib/README.md | 86 ++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/httplib/README.md b/httplib/README.md index 6ec592ab..143bf7d4 100644 --- a/httplib/README.md +++ b/httplib/README.md @@ -6,53 +6,70 @@ httplib is an libs help you to curl remote url. ## GET you can use Get to crawl data. - import "httplib" + import "github.com/astaxie/beego/httplib" str, err := httplib.Get("http://beego.me/").String() if err != nil { - t.Fatal(err) - } + // error + } fmt.Println(str) ## POST POST data to remote url - b:=httplib.Post("http://beego.me/") - b.Param("username","astaxie") - b.Param("password","123456") - str, err := b.String() + req := httplib.Post("http://beego.me/") + req.Param("username","astaxie") + req.Param("password","123456") + str, err := req.String() if err != nil { - t.Fatal(err) - } + // error + } fmt.Println(str) -## set timeout -you can set timeout in request.default is 60 seconds. +## Set timeout -set Get timeout: +The default timeout is `60` seconds, function prototype: + SetTimeout(connectTimeout, readWriteTimeout time.Duration) + +Exmaple: + // GET httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) -set post timeout: - + // POST httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) -- first param is connectTimeout. -- second param is readWriteTimeout -## debug -if you want to debug the request info, set the debug on +## Debug + +If you want to debug the request info, set the debug on httplib.Get("http://beego.me/").Debug(true) -## support HTTPS client -if request url is https. You can set the client support TSL: +## Set HTTP Basic Auth + + str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String() + if err != nil { + // error + } + fmt.Println(str) + +## Set HTTPS + +If request url is https, You can set the client support TSL: httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) -more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config +More info about the `tls.Config` please visit http://golang.org/pkg/crypto/tls/#Config + +## Set HTTP Version + +some servers need to specify the protocol version of HTTP + + httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") -## set cookie +## Set Cookie + some http request need setcookie. So set it like this: cookie := &http.Cookie{} @@ -60,21 +77,20 @@ some http request need setcookie. So set it like this: cookie.Value = "astaxie" httplib.Get("http://beego.me/").SetCookie(cookie) -## upload file -httplib support mutil file upload, use `b.PostFile()` +## Upload file - b:=httplib.Post("http://beego.me/") - b.Param("username","astaxie") - b.Param("password","123456") - b.PostFile("uploadfile1", "httplib.pdf") - b.PostFile("uploadfile2", "httplib.txt") - str, err := b.String() +httplib support mutil file upload, use `req.PostFile()` + + req := httplib.Post("http://beego.me/") + req.Param("username","astaxie") + req.PostFile("uploadfile1", "httplib.pdf") + str, err := req.String() if err != nil { - t.Fatal(err) - } + // error + } fmt.Println(str) -## set HTTP version -some servers need to specify the protocol version of HTTP - httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") \ No newline at end of file +See godoc for further documentation and examples. + +* [godoc.org/github.com/astaxie/beego/httplib](https://godoc.org/github.com/astaxie/beego/httplib) From 6ae8bc1a1689f9b33c96148f691719ae12fabe50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B0=8F=E7=8E=89?= Date: Thu, 21 Aug 2014 00:08:08 +0800 Subject: [PATCH 3/3] Update README --- httplib/README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/httplib/README.md b/httplib/README.md index 143bf7d4..6a72cf7c 100644 --- a/httplib/README.md +++ b/httplib/README.md @@ -10,8 +10,8 @@ you can use Get to crawl data. str, err := httplib.Get("http://beego.me/").String() if err != nil { - // error - } + // error + } fmt.Println(str) ## POST @@ -22,8 +22,8 @@ POST data to remote url req.Param("password","123456") str, err := req.String() if err != nil { - // error - } + // error + } fmt.Println(str) ## Set timeout @@ -32,7 +32,8 @@ The default timeout is `60` seconds, function prototype: SetTimeout(connectTimeout, readWriteTimeout time.Duration) -Exmaple: +Exmaple: + // GET httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) @@ -49,10 +50,10 @@ If you want to debug the request info, set the debug on ## Set HTTP Basic Auth str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String() - if err != nil { - // error - } - fmt.Println(str) + if err != nil { + // error + } + fmt.Println(str) ## Set HTTPS @@ -67,7 +68,7 @@ More info about the `tls.Config` please visit http://golang.org/pkg/crypto/tls/# some servers need to specify the protocol version of HTTP httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") - + ## Set Cookie some http request need setcookie. So set it like this: @@ -86,8 +87,8 @@ httplib support mutil file upload, use `req.PostFile()` req.PostFile("uploadfile1", "httplib.pdf") str, err := req.String() if err != nil { - // error - } + // error + } fmt.Println(str)