Merge pull request #757 from smallfish/develop
Add new function SetBasicAuth, and update README
Showing
3 changed files
with
67 additions
and
32 deletions
| ... | @@ -6,53 +6,71 @@ httplib is an libs help you to curl remote url. | ... | @@ -6,53 +6,71 @@ httplib is an libs help you to curl remote url. |
| 6 | ## GET | 6 | ## GET |
| 7 | you can use Get to crawl data. | 7 | you can use Get to crawl data. |
| 8 | 8 | ||
| 9 | import "httplib" | 9 | import "github.com/astaxie/beego/httplib" |
| 10 | 10 | ||
| 11 | str, err := httplib.Get("http://beego.me/").String() | 11 | str, err := httplib.Get("http://beego.me/").String() |
| 12 | if err != nil { | 12 | if err != nil { |
| 13 | t.Fatal(err) | 13 | // error |
| 14 | } | 14 | } |
| 15 | fmt.Println(str) | 15 | fmt.Println(str) |
| 16 | 16 | ||
| 17 | ## POST | 17 | ## POST |
| 18 | POST data to remote url | 18 | POST data to remote url |
| 19 | 19 | ||
| 20 | b:=httplib.Post("http://beego.me/") | 20 | req := httplib.Post("http://beego.me/") |
| 21 | b.Param("username","astaxie") | 21 | req.Param("username","astaxie") |
| 22 | b.Param("password","123456") | 22 | req.Param("password","123456") |
| 23 | str, err := b.String() | 23 | str, err := req.String() |
| 24 | if err != nil { | 24 | if err != nil { |
| 25 | t.Fatal(err) | 25 | // error |
| 26 | } | 26 | } |
| 27 | fmt.Println(str) | 27 | fmt.Println(str) |
| 28 | 28 | ||
| 29 | ## set timeout | 29 | ## Set timeout |
| 30 | you can set timeout in request.default is 60 seconds. | ||
| 31 | 30 | ||
| 32 | set Get timeout: | 31 | The default timeout is `60` seconds, function prototype: |
| 33 | 32 | ||
| 34 | httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | 33 | SetTimeout(connectTimeout, readWriteTimeout time.Duration) |
| 34 | |||
| 35 | Exmaple: | ||
| 35 | 36 | ||
| 36 | set post timeout: | 37 | // GET |
| 38 | httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | ||
| 37 | 39 | ||
| 40 | // POST | ||
| 38 | httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | 41 | httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) |
| 39 | 42 | ||
| 40 | - first param is connectTimeout. | ||
| 41 | - second param is readWriteTimeout | ||
| 42 | 43 | ||
| 43 | ## debug | 44 | ## Debug |
| 44 | if you want to debug the request info, set the debug on | 45 | |
| 46 | If you want to debug the request info, set the debug on | ||
| 45 | 47 | ||
| 46 | httplib.Get("http://beego.me/").Debug(true) | 48 | httplib.Get("http://beego.me/").Debug(true) |
| 47 | 49 | ||
| 48 | ## support HTTPS client | 50 | ## Set HTTP Basic Auth |
| 49 | if request url is https. You can set the client support TSL: | 51 | |
| 52 | str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String() | ||
| 53 | if err != nil { | ||
| 54 | // error | ||
| 55 | } | ||
| 56 | fmt.Println(str) | ||
| 57 | |||
| 58 | ## Set HTTPS | ||
| 59 | |||
| 60 | If request url is https, You can set the client support TSL: | ||
| 50 | 61 | ||
| 51 | httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) | 62 | httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) |
| 52 | 63 | ||
| 53 | more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config | 64 | More info about the `tls.Config` please visit http://golang.org/pkg/crypto/tls/#Config |
| 65 | |||
| 66 | ## Set HTTP Version | ||
| 67 | |||
| 68 | some servers need to specify the protocol version of HTTP | ||
| 69 | |||
| 70 | httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") | ||
| 71 | |||
| 72 | ## Set Cookie | ||
| 54 | 73 | ||
| 55 | ## set cookie | ||
| 56 | some http request need setcookie. So set it like this: | 74 | some http request need setcookie. So set it like this: |
| 57 | 75 | ||
| 58 | cookie := &http.Cookie{} | 76 | cookie := &http.Cookie{} |
| ... | @@ -60,21 +78,20 @@ some http request need setcookie. So set it like this: | ... | @@ -60,21 +78,20 @@ some http request need setcookie. So set it like this: |
| 60 | cookie.Value = "astaxie" | 78 | cookie.Value = "astaxie" |
| 61 | httplib.Get("http://beego.me/").SetCookie(cookie) | 79 | httplib.Get("http://beego.me/").SetCookie(cookie) |
| 62 | 80 | ||
| 63 | ## upload file | 81 | ## Upload file |
| 64 | httplib support mutil file upload, use `b.PostFile()` | 82 | |
| 83 | httplib support mutil file upload, use `req.PostFile()` | ||
| 65 | 84 | ||
| 66 | b:=httplib.Post("http://beego.me/") | 85 | req := httplib.Post("http://beego.me/") |
| 67 | b.Param("username","astaxie") | 86 | req.Param("username","astaxie") |
| 68 | b.Param("password","123456") | 87 | req.PostFile("uploadfile1", "httplib.pdf") |
| 69 | b.PostFile("uploadfile1", "httplib.pdf") | 88 | str, err := req.String() |
| 70 | b.PostFile("uploadfile2", "httplib.txt") | ||
| 71 | str, err := b.String() | ||
| 72 | if err != nil { | 89 | if err != nil { |
| 73 | t.Fatal(err) | 90 | // error |
| 74 | } | 91 | } |
| 75 | fmt.Println(str) | 92 | fmt.Println(str) |
| 76 | 93 | ||
| 77 | ## set HTTP version | ||
| 78 | some servers need to specify the protocol version of HTTP | ||
| 79 | 94 | ||
| 80 | httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 95 | See godoc for further documentation and examples. | ||
| 96 | |||
| 97 | * [godoc.org/github.com/astaxie/beego/httplib](https://godoc.org/github.com/astaxie/beego/httplib) | ... | ... |
| ... | @@ -147,6 +147,12 @@ func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest | ... | @@ -147,6 +147,12 @@ func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest |
| 147 | return b | 147 | return b |
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | // SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password. | ||
| 151 | func (b *BeegoHttpRequest) SetBasicAuth(username, password string) *BeegoHttpRequest { | ||
| 152 | b.req.SetBasicAuth(username, password) | ||
| 153 | return b | ||
| 154 | } | ||
| 155 | |||
| 150 | // SetEnableCookie sets enable/disable cookiejar | 156 | // SetEnableCookie sets enable/disable cookiejar |
| 151 | func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { | 157 | func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { |
| 152 | b.setting.EnableCookie = enable | 158 | b.setting.EnableCookie = enable | ... | ... |
| ... | @@ -120,6 +120,18 @@ func TestWithCookie(t *testing.T) { | ... | @@ -120,6 +120,18 @@ func TestWithCookie(t *testing.T) { |
| 120 | } | 120 | } |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | func TestWithBasicAuth(t *testing.T) { | ||
| 124 | str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String() | ||
| 125 | if err != nil { | ||
| 126 | t.Fatal(err) | ||
| 127 | } | ||
| 128 | t.Log(str) | ||
| 129 | n := strings.Index(str, "authenticated") | ||
| 130 | if n == -1 { | ||
| 131 | t.Fatal("authenticated not found in response") | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 123 | func TestWithUserAgent(t *testing.T) { | 135 | func TestWithUserAgent(t *testing.T) { |
| 124 | v := "beego" | 136 | v := "beego" |
| 125 | str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String() | 137 | str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String() | ... | ... |
-
Please register or sign in to post a comment