Documentation ¶
Overview ¶
Package httplib is used as http.Client Usage:
import "github.com/astaxie//httplib"
b := httplib.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) more docs http://.me/docs/module/httplib.md
Index ¶
- func SetDefaultSetting(setting HTTPSettings)
- func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)
- 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() (*http.Response, 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) SetBasicAuth(username, password string) *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
- type HTTPSettings
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetDefaultSetting ¶
func SetDefaultSetting(setting HTTPSettings)
SetDefaultSetting Overwrite default settings
Types ¶
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() (*http.Response, 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) 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) 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.
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 EnableCookie bool Gzip bool DumpBody bool }
HTTPSettings is the http.Client setting