mirror of
https://github.com/hanzoai/beego.git
synced 2026-11-17 13:19:25 +00:00
Faithful fork of upstream beego v1.12.12 with two changes only: - module path github.com/beego/beego -> github.com/hanzoai/beego (all internal imports repointed) - orm sqlite3 test driver mattn/go-sqlite3 -> hanzoai/csqlite (drop-in); drops the bogus 'mattn/go-sqlite3 v2.0.3+incompatible' pin that poisoned every consumer's module graph via beego v1's go.mod. go build ./... green; go list -deps mattn = 0; grep mattn go.mod = 0. Behavior identical to upstream v1.12.12 (no logic changes). Lets ai repoint its beego-v1 web layer by import path only, preserving exact runtime behavior.
httplib
httplib is an libs help you to curl remote url.
How to use?
GET
you can use Get to crawl data.
import "github.com/beego/beego/httplib"
str, err := httplib.Get("http://beego.me/").String()
if err != nil {
// error
}
fmt.Println(str)
POST
POST data to remote url
req := httplib.Post("http://beego.me/")
req.Param("username","astaxie")
req.Param("password","123456")
str, err := req.String()
if err != nil {
// error
}
fmt.Println(str)
Set timeout
The default timeout is 60 seconds, function prototype:
SetTimeout(connectTimeout, readWriteTimeout time.Duration)
Example:
// GET
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
// POST
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
Debug
If you want to debug the request info, set the debug on
httplib.Get("http://beego.me/").Debug(true)
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
Set HTTP Version
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:
cookie := &http.Cookie{}
cookie.Name = "username"
cookie.Value = "astaxie"
httplib.Get("http://beego.me/").SetCookie(cookie)
Upload file
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 {
// error
}
fmt.Println(str)
See godoc for further documentation and examples.