Documentation ¶
Overview ¶
Package http is used as http.Client Usage:
b := http.Post("http://.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)
Index ¶
- func SetDefaultSetting(setting HTTPSettings)
- func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)
- type DbType
- type HTTPSettings
- type HttpRequest
- func (b *HttpRequest) Body(data interface{}) *HttpRequest
- func (b *HttpRequest) Bytes() ([]byte, error)
- func (b *HttpRequest) Debug(isdebug bool) *HttpRequest
- func (b *HttpRequest) DoRequest() (resp *http.Response, err error)
- func (b *HttpRequest) DumpBody(isdump bool) *HttpRequest
- func (b *HttpRequest) DumpRequest() []byte
- func (b *HttpRequest) GetRequest() *http.Request
- func (b *HttpRequest) Header(key, value string) *HttpRequest
- func (b *HttpRequest) JSONBody(obj interface{}) (*HttpRequest, error)
- func (b *HttpRequest) Param(key, value string) *HttpRequest
- func (b *HttpRequest) PostFile(formname, filename string) *HttpRequest
- func (b *HttpRequest) Response() (*http.Response, error)
- func (b *HttpRequest) Retries(times int) *HttpRequest
- func (b *HttpRequest) SetBasicAuth(username, password string) *HttpRequest
- func (b *HttpRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *HttpRequest
- func (b *HttpRequest) SetCookie(cookie *http.Cookie) *HttpRequest
- func (b *HttpRequest) SetEnableCookie(enable bool) *HttpRequest
- func (b *HttpRequest) SetHost(host string) *HttpRequest
- func (b *HttpRequest) SetProtocolVersion(vers string) *HttpRequest
- func (b *HttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HttpRequest
- func (b *HttpRequest) SetTLSClientConfig(config *tls.Config) *HttpRequest
- func (b *HttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *HttpRequest
- func (b *HttpRequest) SetTransport(transport http.RoundTripper) *HttpRequest
- func (b *HttpRequest) SetUserAgent(useragent string) *HttpRequest
- func (b *HttpRequest) Setting(setting HTTPSettings) *HttpRequest
- func (b *HttpRequest) String() (string, error)
- func (b *HttpRequest) ToFile(filename string) error
- func (b *HttpRequest) ToJSON(v interface{}) error
- func (b *HttpRequest) ToXML(v interface{}) error
- func (b *HttpRequest) ToYAML(v interface{}) error
- func (b *HttpRequest) XMLBody(obj interface{}) (*HttpRequest, error)
- func (b *HttpRequest) YAMLBody(obj interface{}) (*HttpRequest, error)
- type URI
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaultSetting ¶
func SetDefaultSetting(setting HTTPSettings)
SetDefaultSetting Overwrite default settings
Types ¶
type HTTPSettings ¶
type HTTPSettings struct { ShowDebug bool UserAgent string ConnectTimeout time.Duration ReadWriteTimeout time.Duration TLSClientConfig *tls.Config Proxy func(*http.Request) (*url.URL, error) Transport http.RoundTripper CheckRedirect func(req *http.Request, via []*http.Request) error EnableCookie bool Gzip bool DumpBody bool Retries int // if set to -1 means will retry forever }
HTTPSettings is the http.Client setting
type HttpRequest ¶
type HttpRequest struct {
// contains filtered or unexported fields
}
HttpRequest provides more useful methods for requesting one url than http.Request.
func NewRequest ¶
func NewRequest(rawurl, method string) *HttpRequest
NewRequest return *HttpRequest with specific method
func (*HttpRequest) Body ¶
func (b *HttpRequest) Body(data interface{}) *HttpRequest
Body adds request raw body. it supports string and []byte.
func (*HttpRequest) Bytes ¶
func (b *HttpRequest) Bytes() ([]byte, error)
Bytes returns the body []byte in response. it calls Response inner.
func (*HttpRequest) Debug ¶
func (b *HttpRequest) Debug(isdebug bool) *HttpRequest
Debug sets show debug or not when executing request.
func (*HttpRequest) DoRequest ¶
func (b *HttpRequest) DoRequest() (resp *http.Response, err error)
DoRequest will do the client.Do
func (*HttpRequest) DumpBody ¶
func (b *HttpRequest) DumpBody(isdump bool) *HttpRequest
DumpBody setting whether need to Dump the Body.
func (*HttpRequest) DumpRequest ¶
func (b *HttpRequest) DumpRequest() []byte
DumpRequest return the DumpRequest
func (*HttpRequest) GetRequest ¶
func (b *HttpRequest) GetRequest() *http.Request
GetRequest return the request object
func (*HttpRequest) Header ¶
func (b *HttpRequest) Header(key, value string) *HttpRequest
Header add header item string in request.
func (*HttpRequest) JSONBody ¶
func (b *HttpRequest) JSONBody(obj interface{}) (*HttpRequest, error)
JSONBody adds request raw body encoding by JSON.
func (*HttpRequest) Param ¶
func (b *HttpRequest) Param(key, value string) *HttpRequest
Param adds query param in to request. params build query string as ?key1=value1&key2=value2...
func (*HttpRequest) PostFile ¶
func (b *HttpRequest) PostFile(formname, filename string) *HttpRequest
PostFile add a post file to the request
func (*HttpRequest) Response ¶
func (b *HttpRequest) Response() (*http.Response, error)
Response executes request client gets response mannually.
func (*HttpRequest) Retries ¶
func (b *HttpRequest) Retries(times int) *HttpRequest
Retries sets Retries times. default is 0 means no retried. -1 means retried forever. others means retried times.
func (*HttpRequest) SetBasicAuth ¶
func (b *HttpRequest) SetBasicAuth(username, password string) *HttpRequest
SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
func (*HttpRequest) SetCheckRedirect ¶
func (b *HttpRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *HttpRequest
SetCheckRedirect specifies the policy for handling redirects.
If CheckRedirect is nil, the Client uses its default policy, which is to stop after 10 consecutive requests.
func (*HttpRequest) SetCookie ¶
func (b *HttpRequest) SetCookie(cookie *http.Cookie) *HttpRequest
SetCookie add cookie into request.
func (*HttpRequest) SetEnableCookie ¶
func (b *HttpRequest) SetEnableCookie(enable bool) *HttpRequest
SetEnableCookie sets enable/disable cookiejar
func (*HttpRequest) SetHost ¶
func (b *HttpRequest) SetHost(host string) *HttpRequest
SetHost set the request host
func (*HttpRequest) SetProtocolVersion ¶
func (b *HttpRequest) SetProtocolVersion(vers string) *HttpRequest
SetProtocolVersion Set the protocol version for incoming requests. Client requests always use HTTP/1.1.
func (*HttpRequest) SetProxy ¶
func (b *HttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HttpRequest
SetProxy set the http proxy example:
func(req *http.Request) (*url.URL, error) { u, _ := url.ParseRequestURI("http://127.0.0.1:8118") return u, nil }
func (*HttpRequest) SetTLSClientConfig ¶
func (b *HttpRequest) SetTLSClientConfig(config *tls.Config) *HttpRequest
SetTLSClientConfig sets tls connection configurations if visiting https url.
func (*HttpRequest) SetTimeout ¶
func (b *HttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *HttpRequest
SetTimeout sets connect time out and read-write time out for Request.
func (*HttpRequest) SetTransport ¶
func (b *HttpRequest) SetTransport(transport http.RoundTripper) *HttpRequest
SetTransport set the setting transport
func (*HttpRequest) SetUserAgent ¶
func (b *HttpRequest) SetUserAgent(useragent string) *HttpRequest
SetUserAgent sets User-Agent header field
func (*HttpRequest) Setting ¶
func (b *HttpRequest) Setting(setting HTTPSettings) *HttpRequest
Setting Change request settings
func (*HttpRequest) String ¶
func (b *HttpRequest) String() (string, error)
String returns the body string in response. it calls Response inner.
func (*HttpRequest) ToFile ¶
func (b *HttpRequest) ToFile(filename string) error
ToFile saves the body data in response to one file. it calls Response inner.
func (*HttpRequest) ToJSON ¶
func (b *HttpRequest) ToJSON(v interface{}) error
ToJSON returns the map that marshals from the body bytes as json in response . it calls Response inner.
func (*HttpRequest) ToXML ¶
func (b *HttpRequest) ToXML(v interface{}) error
ToXML returns the map that marshals from the body bytes as xml in response . it calls Response inner.
func (*HttpRequest) ToYAML ¶
func (b *HttpRequest) ToYAML(v interface{}) error
ToYAML returns the map that marshals from the body bytes as yaml in response . it calls Response inner.
func (*HttpRequest) XMLBody ¶
func (b *HttpRequest) XMLBody(obj interface{}) (*HttpRequest, error)
XMLBody adds request raw body encoding by XML.
func (*HttpRequest) YAMLBody ¶
func (b *HttpRequest) YAMLBody(obj interface{}) (*HttpRequest, error)
YAMLBody adds request raw body encoding by YAML.