mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 21:50:55 +00:00
Update README
This commit is contained in:
parent
9a583323a8
commit
e70537f8b3
@ -6,53 +6,70 @@ httplib is an libs help you to curl remote url.
|
|||||||
## GET
|
## GET
|
||||||
you can use Get to crawl data.
|
you can use Get to crawl data.
|
||||||
|
|
||||||
import "httplib"
|
import "github.com/astaxie/beego/httplib"
|
||||||
|
|
||||||
str, err := httplib.Get("http://beego.me/").String()
|
str, err := httplib.Get("http://beego.me/").String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
// error
|
||||||
}
|
}
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
|
|
||||||
## POST
|
## POST
|
||||||
POST data to remote url
|
POST data to remote url
|
||||||
|
|
||||||
b:=httplib.Post("http://beego.me/")
|
req := httplib.Post("http://beego.me/")
|
||||||
b.Param("username","astaxie")
|
req.Param("username","astaxie")
|
||||||
b.Param("password","123456")
|
req.Param("password","123456")
|
||||||
str, err := b.String()
|
str, err := req.String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
// error
|
||||||
}
|
}
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
|
|
||||||
## set timeout
|
## Set timeout
|
||||||
you can set timeout in request.default is 60 seconds.
|
|
||||||
|
|
||||||
set Get timeout:
|
The default timeout is `60` seconds, function prototype:
|
||||||
|
|
||||||
|
SetTimeout(connectTimeout, readWriteTimeout time.Duration)
|
||||||
|
|
||||||
|
Exmaple:
|
||||||
|
// GET
|
||||||
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
|
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
|
||||||
|
|
||||||
set post timeout:
|
// POST
|
||||||
|
|
||||||
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
|
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
|
||||||
|
|
||||||
- first param is connectTimeout.
|
|
||||||
- second param is readWriteTimeout
|
|
||||||
|
|
||||||
## debug
|
## Debug
|
||||||
if you want to debug the request info, set the debug on
|
|
||||||
|
If you want to debug the request info, set the debug on
|
||||||
|
|
||||||
httplib.Get("http://beego.me/").Debug(true)
|
httplib.Get("http://beego.me/").Debug(true)
|
||||||
|
|
||||||
## support HTTPS client
|
## Set HTTP Basic Auth
|
||||||
if request url is https. You can set the client support TSL:
|
|
||||||
|
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})
|
httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
|
||||||
|
|
||||||
more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
|
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
|
||||||
|
|
||||||
## set cookie
|
|
||||||
some http request need setcookie. So set it like this:
|
some http request need setcookie. So set it like this:
|
||||||
|
|
||||||
cookie := &http.Cookie{}
|
cookie := &http.Cookie{}
|
||||||
@ -60,21 +77,20 @@ some http request need setcookie. So set it like this:
|
|||||||
cookie.Value = "astaxie"
|
cookie.Value = "astaxie"
|
||||||
httplib.Get("http://beego.me/").SetCookie(cookie)
|
httplib.Get("http://beego.me/").SetCookie(cookie)
|
||||||
|
|
||||||
## upload file
|
## Upload file
|
||||||
httplib support mutil file upload, use `b.PostFile()`
|
|
||||||
|
|
||||||
b:=httplib.Post("http://beego.me/")
|
httplib support mutil file upload, use `req.PostFile()`
|
||||||
b.Param("username","astaxie")
|
|
||||||
b.Param("password","123456")
|
req := httplib.Post("http://beego.me/")
|
||||||
b.PostFile("uploadfile1", "httplib.pdf")
|
req.Param("username","astaxie")
|
||||||
b.PostFile("uploadfile2", "httplib.txt")
|
req.PostFile("uploadfile1", "httplib.pdf")
|
||||||
str, err := b.String()
|
str, err := req.String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
// error
|
||||||
}
|
}
|
||||||
fmt.Println(str)
|
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")
|
See godoc for further documentation and examples.
|
||||||
|
|
||||||
|
* [godoc.org/github.com/astaxie/beego/httplib](https://godoc.org/github.com/astaxie/beego/httplib)
|
||||||
|
Loading…
Reference in New Issue
Block a user