1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:10:56 +00:00
Beego/httplib/README.md

98 lines
2.1 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.
2014-08-20 16:03:03 +00:00
import "github.com/astaxie/beego/httplib"
2013-11-28 03:26:17 +00:00
str, err := httplib.Get("http://beego.me/").String()
if err != nil {
2014-08-20 16:08:08 +00:00
// error
}
2013-11-28 03:26:17 +00:00
fmt.Println(str)
## POST
POST data to remote url
2014-08-20 16:03:03 +00:00
req := httplib.Post("http://beego.me/")
req.Param("username","astaxie")
req.Param("password","123456")
str, err := req.String()
2013-11-28 03:26:17 +00:00
if err != nil {
2014-08-20 16:08:08 +00:00
// error
}
2013-11-28 03:26:17 +00:00
fmt.Println(str)
2014-08-20 16:03:03 +00:00
## Set timeout
2013-11-28 03:26:17 +00:00
2014-08-20 16:03:03 +00:00
The default timeout is `60` seconds, function prototype:
2013-11-28 03:26:17 +00:00
2014-08-20 16:03:03 +00:00
SetTimeout(connectTimeout, readWriteTimeout time.Duration)
2017-04-28 15:37:40 +00:00
Example:
2014-08-20 16:08:08 +00:00
2014-08-20 16:03:03 +00:00
// GET
2013-11-28 03:26:17 +00:00
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
2014-08-20 16:03:03 +00:00
// POST
2013-11-28 03:26:17 +00:00
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
2014-08-20 16:03:03 +00:00
## Debug
If you want to debug the request info, set the debug on
2013-11-28 03:26:17 +00:00
2013-12-12 07:23:17 +00:00
httplib.Get("http://beego.me/").Debug(true)
2013-12-10 14:01:50 +00:00
2014-08-20 16:03:03 +00:00
## Set HTTP Basic Auth
str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String()
2014-08-20 16:08:08 +00:00
if err != nil {
// error
}
fmt.Println(str)
2014-08-20 16:03:03 +00:00
## Set HTTPS
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})
2014-08-20 16:03:03 +00:00
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")
2014-08-20 16:08:08 +00:00
2014-08-20 16:03:03 +00:00
## Set Cookie
2013-12-12 07:23:17 +00:00
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-08-20 16:03:03 +00:00
## Upload file
2014-05-08 08:58:08 +00:00
2014-08-20 16:03:03 +00:00
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()
2014-05-08 08:58:08 +00:00
if err != nil {
2014-08-20 16:08:08 +00:00
// error
}
2014-05-08 08:58:08 +00:00
fmt.Println(str)
2014-08-20 16:03:03 +00:00
See godoc for further documentation and examples.
* [godoc.org/github.com/astaxie/beego/httplib](https://godoc.org/github.com/astaxie/beego/httplib)