# README
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/v2/client/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.
# Functions
AddDefaultFilter add a new filter into defaultSetting Be careful about using this method if you invoke SetDefaultSetting somewhere.
Delete returns *BeegoHttpRequest DELETE method.
Get returns *BeegoHttpRequest with GET method.
GetDefaultSetting return current default setting.
Head returns *BeegoHttpRequest with HEAD method.
NewBeegoRequest returns *BeegoHttpRequest with specific method TODO add error as return value I think if we don't return error users are hard to check whether we create Beego request successfully.
NewClient return a new http client.
NewHttpResponseWithJsonBody will try to convert the data to json format usually you only use this when you want to mock http Response.
Post returns *BeegoHttpRequest with POST method.
Put returns *BeegoHttpRequest with PUT method.
SetDefaultSetting overwrites default settings Keep in mind that when you invoke the SetDefaultSetting some methods invoked before SetDefaultSetting.
TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.
No description provided by the author
WithBasicAuth adds a custom function to set basic auth.
WithCheckRedirect will specifies the policy for handling redirects in all subsequent request.
WithContentType adds ContentType in header.
WithCookie adds a cookie to the request.
WithEnableCookie will enable cookie in all subsequent request.
WithEnableGzip will enable gzip in all subsequent request.
WithFilters will use the filter as the invocation filters.
WithHeader adds header item string in request.
WithHTTPSetting can replace beegoHTTPSeting.
WithParam adds query param in to request.
WithProxy will set http proxy field in all subsequent request.
WithRetry set retry times and delay for the request default is 0 (never retry) -1 retry indefinitely (forever) Other numbers specify the exact retry amount.
WithTimeout sets connect time out and read-write time out for BeegoRequest.
WithTLSClientConfig will adds tls config in all subsequent request.
Withtokenfactory adds a custom function to set Authorization.
WithTransport will set transport field in all subsequent request.
WithEnableCookie will adds UA in all subsequent request.
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
BeegoHTTPRequest provides more useful methods than http.Request for requesting a url.
BeegoHTTPSettings is the http.Client setting.
Client provides an HTTP client supporting chain call.
# Interfaces
HTTPBodyCarrier If value implement HTTPBodyCarrier.
HTTPBytesCarrier If value implement HTTPBytesCarrier.
HttpHeaderCarrier If value implement HttpHeaderCarrier.
HTTPResponseCarrier If value implement HTTPResponseCarrier.
HTTPStatusCarrier If value implement HTTPStatusCarrier.
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author