Update README
Showing
1 changed file
with
51 additions
and
35 deletions
| ... | @@ -6,53 +6,70 @@ httplib is an libs help you to curl remote url. | ... | @@ -6,53 +6,70 @@ 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 | ||
| 33 | SetTimeout(connectTimeout, readWriteTimeout time.Duration) | ||
| 34 | |||
| 35 | Exmaple: | ||
| 36 | // GET | ||
| 34 | httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | 37 | httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) |
| 35 | 38 | ||
| 36 | set post timeout: | 39 | // POST |
| 37 | |||
| 38 | httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) | 40 | httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second) |
| 39 | 41 | ||
| 40 | - first param is connectTimeout. | ||
| 41 | - second param is readWriteTimeout | ||
| 42 | 42 | ||
| 43 | ## debug | 43 | ## Debug |
| 44 | if you want to debug the request info, set the debug on | 44 | |
| 45 | If you want to debug the request info, set the debug on | ||
| 45 | 46 | ||
| 46 | httplib.Get("http://beego.me/").Debug(true) | 47 | httplib.Get("http://beego.me/").Debug(true) |
| 47 | 48 | ||
| 48 | ## support HTTPS client | 49 | ## Set HTTP Basic Auth |
| 49 | if request url is https. You can set the client support TSL: | 50 | |
| 51 | str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String() | ||
| 52 | if err != nil { | ||
| 53 | // error | ||
| 54 | } | ||
| 55 | fmt.Println(str) | ||
| 56 | |||
| 57 | ## Set HTTPS | ||
| 58 | |||
| 59 | If request url is https, You can set the client support TSL: | ||
| 50 | 60 | ||
| 51 | httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) | 61 | httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) |
| 52 | 62 | ||
| 53 | more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config | 63 | More info about the `tls.Config` please visit http://golang.org/pkg/crypto/tls/#Config |
| 64 | |||
| 65 | ## Set HTTP Version | ||
| 66 | |||
| 67 | some servers need to specify the protocol version of HTTP | ||
| 68 | |||
| 69 | httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") | ||
| 54 | 70 | ||
| 55 | ## set cookie | 71 | ## Set Cookie |
| 72 | |||
| 56 | some http request need setcookie. So set it like this: | 73 | some http request need setcookie. So set it like this: |
| 57 | 74 | ||
| 58 | cookie := &http.Cookie{} | 75 | cookie := &http.Cookie{} |
| ... | @@ -60,21 +77,20 @@ some http request need setcookie. So set it like this: | ... | @@ -60,21 +77,20 @@ some http request need setcookie. So set it like this: |
| 60 | cookie.Value = "astaxie" | 77 | cookie.Value = "astaxie" |
| 61 | httplib.Get("http://beego.me/").SetCookie(cookie) | 78 | httplib.Get("http://beego.me/").SetCookie(cookie) |
| 62 | 79 | ||
| 63 | ## upload file | 80 | ## Upload file |
| 64 | httplib support mutil file upload, use `b.PostFile()` | ||
| 65 | 81 | ||
| 66 | b:=httplib.Post("http://beego.me/") | 82 | httplib support mutil file upload, use `req.PostFile()` |
| 67 | b.Param("username","astaxie") | 83 | |
| 68 | b.Param("password","123456") | 84 | req := httplib.Post("http://beego.me/") |
| 69 | b.PostFile("uploadfile1", "httplib.pdf") | 85 | req.Param("username","astaxie") |
| 70 | b.PostFile("uploadfile2", "httplib.txt") | 86 | req.PostFile("uploadfile1", "httplib.pdf") |
| 71 | str, err := b.String() | 87 | str, err := req.String() |
| 72 | if err != nil { | 88 | if err != nil { |
| 73 | t.Fatal(err) | 89 | // error |
| 74 | } | 90 | } |
| 75 | fmt.Println(str) | 91 | fmt.Println(str) |
| 76 | 92 | ||
| 77 | ## set HTTP version | ||
| 78 | some servers need to specify the protocol version of HTTP | ||
| 79 | 93 | ||
| 80 | httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1") | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 94 | See godoc for further documentation and examples. | ||
| 95 | |||
| 96 | * [godoc.org/github.com/astaxie/beego/httplib](https://godoc.org/github.com/astaxie/beego/httplib) | ... | ... |
-
Please register or sign in to post a comment