1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-26 03:54:13 +00:00
Beego/httplib/README.md

80 lines
1.8 KiB
Markdown
Raw Normal View History

2013-11-28 03:26:17 +00:00
# httplib
httplib is an libs help you to curl remote url.
# How to use?
## GET
you can use Get to crawl data.
import "httplib"
str, err := httplib.Get("http://beego.me/").String()
if err != nil {
t.Fatal(err)
}
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()
if err != nil {
t.Fatal(err)
}
fmt.Println(str)
## set timeout
you can set timeout in request.default is 60 seconds.
set Get timeout:
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
set post timeout:
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
2013-12-12 07:23:17 +00:00
httplib.Get("http://beego.me/").Debug(true)
2013-12-10 14:01:50 +00:00
## support HTTPS client
2013-12-12 07:23:17 +00:00
if request url is https. You can set the client support TSL:
2013-12-10 14:01:50 +00:00
httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
2013-12-11 12:50:41 +00:00
2013-12-12 07:23:17 +00:00
## set cookie
some http request need setcookie. So set it like this:
cookie := &http.Cookie{}
cookie.Name = "username"
cookie.Value = "astaxie"
httplib.Get("http://beego.me/").SetCookie(cookie)
2014-05-08 08:58:08 +00:00
## upload file
httplib support mutil file upload, use `b.PostFile()`
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()
if err != nil {
t.Fatal(err)
}
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")