Documentation ¶
Index ¶
- Variables
- func Close()
- func CloseResponse(resp *http.Response)
- func Download(u string, timeout time.Duration, queryData, header map[string]string) (data []byte, resp *http.Response, err error)
- func DownloadToFile(u string, timeout time.Duration, queryData, header map[string]string, ...) (resp *http.Response, err error)
- func DownloadToWriter(u string, timeout time.Duration, queryData, header map[string]string, ...) (resp *http.Response, err error)
- func Get(u string, timeout time.Duration, queryData, header map[string]string) (body []byte, code int, resp *http.Response, err error)
- func GetContent(_url string, timeout time.Duration) []byte
- func GetContentE(_url string, timeout time.Duration) ([]byte, error)
- func GetResponseBody(resp *http.Response) []byte
- func GetResponseBodyE(resp *http.Response) ([]byte, error)
- func IsFormMethod(method string) bool
- func IsFormRequest(req *http.Request) bool
- func NewGet(URL string, timeout time.Duration, queryData, header map[string]string) (req *http.Request, cancel context.CancelFunc, err error)
- func NewGetWithContext(ctx context.Context, URL string, queryData, header map[string]string) (req *http.Request, err error)
- func NewPost(URL string, timeout time.Duration, data, header map[string]string) (req *http.Request, cancel context.CancelFunc, err error)
- func NewPostReaderWithContext(ctx context.Context, URL string, r io.Reader, header map[string]string) (req *http.Request, err error)
- func NewPostWithContext(ctx context.Context, URL string, data, header map[string]string) (req *http.Request, err error)
- func NewRequest(method, URL string, timeout time.Duration, data, header map[string]string) (req *http.Request, cancel context.CancelFunc, err error)
- func NewRequestWithContext(ctx context.Context, method, URL string, data, header map[string]string) (req *http.Request, err error)
- func Post(u string, data map[string]string, timeout time.Duration, ...) (body []byte, code int, resp *http.Response, err error)
- func PostOfReader(u string, r io.Reader, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)
- func Upload(u, fieldName, filename string, data map[string]string, timeout time.Duration, ...) (body string, resp *http.Response, err error)
- func UploadOfReader(u, fieldName string, filename string, reader io.ReadCloser, ...) (body string, resp *http.Response, err error)
- type AfterDoClientFunc
- type AfterDoFunc
- type BatchRequest
- func NewBatchGet(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)
- func NewBatchPost(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)
- func NewBatchRequest(reqArr []*http.Request, client *http.Client) *BatchRequest
- func NewBatchURL(client *http.Client, method string, urlArr []string, timeout time.Duration, ...) (tr *BatchRequest, err error)
- func (s *BatchRequest) AppendAfterDo(afterDo AfterDoFunc) *BatchRequest
- func (s *BatchRequest) AppendBeforeDo(beforeDo BeforeDoFunc) *BatchRequest
- func (s *BatchRequest) CheckErrorFunc(checkErrorFunc func(idx int, req *http.Request, resp *http.Response) error) *BatchRequest
- func (s *BatchRequest) Close() *BatchRequest
- func (s *BatchRequest) DoFunc(doFunc func(idx int, req *http.Request) (*http.Response, error)) *BatchRequest
- func (s *BatchRequest) Err() error
- func (s *BatchRequest) ErrAll() (errs []error)
- func (s *BatchRequest) ErrorCount() int
- func (s *BatchRequest) Execute() *BatchRequest
- func (s *BatchRequest) Keepalive(keepalive bool) *BatchRequest
- func (s *BatchRequest) MaxTry(maxTry int) *BatchRequest
- func (s *BatchRequest) Pool(pool *gpool.Pool) *BatchRequest
- func (s *BatchRequest) Resp() *Response
- func (s *BatchRequest) RespAll() (responseAll []*Response)
- func (s *BatchRequest) SetAfterDo(afterDo AfterDoFunc) *BatchRequest
- func (s *BatchRequest) SetBeforeDo(beforeDo BeforeDoFunc) *BatchRequest
- func (s *BatchRequest) Success() bool
- func (s *BatchRequest) WaitFirstSuccess() *BatchRequest
- type BeforeDoClientFunc
- type BeforeDoFunc
- type CookieJar
- type HTTPClient
- func (s *HTTPClient) AppendAfterDo(afterDo AfterDoClientFunc) *HTTPClient
- func (s *HTTPClient) AppendBeforeDo(beforeDo BeforeDoClientFunc) *HTTPClient
- func (s *HTTPClient) Close()
- func (s *HTTPClient) Do(req *http.Request, timeout time.Duration) (resp *http.Response, err error)
- func (s *HTTPClient) Download(u string, timeout time.Duration, queryData, header map[string]string) (data []byte, resp *http.Response, err error)
- func (s *HTTPClient) DownloadToFile(u string, timeout time.Duration, queryData, header map[string]string, ...) (resp *http.Response, err error)
- func (s *HTTPClient) DownloadToWriter(u string, timeout time.Duration, queryData, header map[string]string, ...) (resp *http.Response, err error)
- func (s *HTTPClient) Get(u string, timeout time.Duration, queryData, header map[string]string) (body []byte, code int, resp *http.Response, err error)
- func (s *HTTPClient) NewBatchGet(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)
- func (s *HTTPClient) NewBatchPost(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)
- func (s *HTTPClient) NewTriableGet(URL string, maxTry int, timeout time.Duration, ...) (tr *TriableRequest, err error)
- func (s *HTTPClient) NewTriablePost(URL string, maxTry int, timeout time.Duration, data, header map[string]string) (tr *TriableRequest, err error)
- func (s *HTTPClient) Post(u string, data map[string]string, timeout time.Duration, ...) (body []byte, code int, resp *http.Response, err error)
- func (s *HTTPClient) PostOfReader(u string, r io.Reader, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)
- func (s *HTTPClient) ProxyUsed() *url.URL
- func (s *HTTPClient) SetAfterDo(afterDo AfterDoClientFunc) *HTTPClient
- func (s *HTTPClient) SetBasicAuth(username, password string)
- func (s *HTTPClient) SetBeforeDo(beforeDo BeforeDoClientFunc) *HTTPClient
- func (s *HTTPClient) SetClientCert(certPemBytes, keyBytes []byte) (err error)
- func (s *HTTPClient) SetConnWrap(fn func(c net.Conn) (conn net.Conn, err error))
- func (s *HTTPClient) SetDNS(dns ...string) (err error)
- func (s *HTTPClient) SetDialer(dialer func(network, address string, timeout time.Duration) (net.Conn, error))
- func (s *HTTPClient) SetHttpClientFactory(httpClientFactory func(r *http.Request) *http.Client)
- func (s *HTTPClient) SetKeepalive(keepalive bool)
- func (s *HTTPClient) SetPinCert(pemBytes []byte) (err error)
- func (s *HTTPClient) SetPreHandler(preHandler func(r *http.Request))
- func (s *HTTPClient) SetProxy(proxyURL string) (err error)
- func (s *HTTPClient) SetProxyFromEnv(set bool)
- func (s *HTTPClient) SetRootCaCerts(caPemBytes ...[]byte) (err error)
- func (s *HTTPClient) Upload(u, fieldName, filename string, data map[string]string, timeout time.Duration, ...) (body string, resp *http.Response, err error)
- func (s *HTTPClient) UploadOfReader(u, fieldName string, filename string, reader io.ReadCloser, ...) (body string, resp *http.Response, err error)
- type Response
- func (s *Response) Body() []byte
- func (s *Response) BodyE() ([]byte, error)
- func (s *Response) Close()
- func (s *Response) EndTime() time.Time
- func (s *Response) Err() error
- func (s *Response) Idx() int
- func (s *Response) Request() *http.Request
- func (s *Response) StartTime() time.Time
- func (s *Response) UsedTime() time.Duration
- type TriableRequest
- func NewTriableGet(URL string, maxTry int, timeout time.Duration, ...) (tr *TriableRequest, err error)
- func NewTriablePost(URL string, maxTry int, timeout time.Duration, data, header map[string]string) (tr *TriableRequest, err error)
- func NewTriableRequest(client *http.Client, req *http.Request, maxTry int, timeout time.Duration) *TriableRequest
- func NewTriableRequestByURL(client *http.Client, method string, URL string, maxTry int, ...) (tr *TriableRequest, err error)
- func (s *TriableRequest) AfterDo(afterDo AfterDoFunc) *TriableRequest
- func (s *TriableRequest) AppendBeforeDo(beforeDo BeforeDoFunc) *TriableRequest
- func (s *TriableRequest) CheckErrorFunc(checkErrorFunc func(idx int, req *http.Request, resp *http.Response) error) *TriableRequest
- func (s *TriableRequest) Close() *TriableRequest
- func (s *TriableRequest) DoFunc(doFunc func(req *http.Request) (*http.Response, error)) *TriableRequest
- func (s *TriableRequest) Err() error
- func (s *TriableRequest) ErrAll() []error
- func (s *TriableRequest) Execute() *Response
- func (s *TriableRequest) Keepalive(keepalive bool) *TriableRequest
- func (s *TriableRequest) SetBeforeDo(beforeDo BeforeDoFunc) *TriableRequest
Constants ¶
This section is empty.
Variables ¶
var ( // Client a HTTPClient, all request shared one http.Client object, keep cookies, keepalive etc. Client = NewHTTPClient() )
Functions ¶
func CloseResponse ¶
func DownloadToFile ¶
func DownloadToWriter ¶
func GetResponseBody ¶
func IsFormMethod ¶
func IsFormRequest ¶
func NewGetWithContext ¶
func NewPostWithContext ¶
func NewRequest ¶
func NewRequestWithContext ¶
func PostOfReader ¶
Types ¶
type AfterDoClientFunc ¶
type AfterDoFunc ¶
type AfterDoFunc func(resp *Response)
type BatchRequest ¶
type BatchRequest struct {
// contains filtered or unexported fields
}
func NewBatchGet ¶
func NewBatchPost ¶
func NewBatchRequest ¶
func NewBatchRequest(reqArr []*http.Request, client *http.Client) *BatchRequest
NewBatchRequest new a BatchRequest by []*http.Request.
func NewBatchURL ¶
func NewBatchURL(client *http.Client, method string, urlArr []string, timeout time.Duration, data, header map[string]string) (tr *BatchRequest, err error)
NewBatchURL new a BatchRequest by url slice []string.
func (*BatchRequest) AppendAfterDo ¶
func (s *BatchRequest) AppendAfterDo(afterDo AfterDoFunc) *BatchRequest
AppendAfterDo add a callback call after request sent.
func (*BatchRequest) AppendBeforeDo ¶
func (s *BatchRequest) AppendBeforeDo(beforeDo BeforeDoFunc) *BatchRequest
AppendBeforeDo add a callback call before request sent.
func (*BatchRequest) CheckErrorFunc ¶
func (s *BatchRequest) CheckErrorFunc(checkErrorFunc func(idx int, req *http.Request, resp *http.Response) error) *BatchRequest
CheckErrorFunc if returns an error, the request treat as fail.
func (*BatchRequest) Close ¶
func (s *BatchRequest) Close() *BatchRequest
Close close all response body can context cancel func.
func (*BatchRequest) DoFunc ¶
func (s *BatchRequest) DoFunc(doFunc func(idx int, req *http.Request) (*http.Response, error)) *BatchRequest
DoFunc sets a request sender.
func (*BatchRequest) Err ¶
func (s *BatchRequest) Err() error
Err returns first error of fail requests.
func (*BatchRequest) ErrAll ¶
func (s *BatchRequest) ErrAll() (errs []error)
ErrAll returns all errors of fail requests.
func (*BatchRequest) ErrorCount ¶
func (s *BatchRequest) ErrorCount() int
ErrorCount returns count of fail requests.
func (*BatchRequest) Execute ¶
func (s *BatchRequest) Execute() *BatchRequest
Execute batch send requests,
In default Execute will wait all request done. If you want to get the first success response,
using BatchRequest.WaitFirstSuccess().Execute(), Execute() will return immediately when get a success response.
If all requests are fail, Execute() return after all requests done.
func (*BatchRequest) Keepalive ¶
func (s *BatchRequest) Keepalive(keepalive bool) *BatchRequest
Keepalive sets enable or disable for request keepalive
func (*BatchRequest) MaxTry ¶
func (s *BatchRequest) MaxTry(maxTry int) *BatchRequest
MaxTry sets the max retry count when a request error occurred.
func (*BatchRequest) Pool ¶
func (s *BatchRequest) Pool(pool *gpool.Pool) *BatchRequest
Pool sets a *gpool.Pool to execute request. In default goroutine will be used.
func (*BatchRequest) Resp ¶
func (s *BatchRequest) Resp() *Response
Resp returns first success response
func (*BatchRequest) RespAll ¶
func (s *BatchRequest) RespAll() (responseAll []*Response)
RespAll returns all request's response, if you want to get if the response is success, checking if Response.Err() is nil.
func (*BatchRequest) SetAfterDo ¶
func (s *BatchRequest) SetAfterDo(afterDo AfterDoFunc) *BatchRequest
SetAfterDo sets callback call after request sent.
func (*BatchRequest) SetBeforeDo ¶
func (s *BatchRequest) SetBeforeDo(beforeDo BeforeDoFunc) *BatchRequest
SetBeforeDo sets callback call before request sent.
func (*BatchRequest) Success ¶
func (s *BatchRequest) Success() bool
Success returns requests at least one is success in waitFirstSuccess mode. returns all requests are success in non waitFirstSuccess mode.
func (*BatchRequest) WaitFirstSuccess ¶
func (s *BatchRequest) WaitFirstSuccess() *BatchRequest
WaitFirstSuccess sets Execute() return immediately when get a success response.
type BeforeDoClientFunc ¶
type BeforeDoFunc ¶
type CookieJar ¶
type CookieJar struct {
// contains filtered or unexported fields
}
func NewCookieJar ¶
func NewCookieJar() *CookieJar
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
HTTPClient do http get, post etc.
func NewHTTPClient ¶
func NewHTTPClient() *HTTPClient
NewHTTPClient new a HTTPClient, all request shared one http.Client object, keep cookies, keepalive etc. Keepalive is enabled in default.
func (*HTTPClient) AppendAfterDo ¶
func (s *HTTPClient) AppendAfterDo(afterDo AfterDoClientFunc) *HTTPClient
AppendAfterDo add a callback call after request sent.
func (*HTTPClient) AppendBeforeDo ¶
func (s *HTTPClient) AppendBeforeDo(beforeDo BeforeDoClientFunc) *HTTPClient
AppendBeforeDo add a callback call before request sent.
func (*HTTPClient) Close ¶
func (s *HTTPClient) Close()
func (*HTTPClient) Download ¶
func (s *HTTPClient) Download(u string, timeout time.Duration, queryData, header map[string]string) (data []byte, resp *http.Response, err error)
Download gets url bytes contents.
func (*HTTPClient) DownloadToFile ¶
func (s *HTTPClient) DownloadToFile(u string, timeout time.Duration, queryData, header map[string]string, file string) (resp *http.Response, err error)
DownloadToFile gets url bytes contents and save to the file.
func (*HTTPClient) DownloadToWriter ¶
func (s *HTTPClient) DownloadToWriter(u string, timeout time.Duration, queryData, header map[string]string, writer io.Writer) (resp *http.Response, err error)
DownloadToWriter gets url bytes contents and copy to the writer at realtime.
func (*HTTPClient) Get ¶
func (s *HTTPClient) Get(u string, timeout time.Duration, queryData, header map[string]string) (body []byte, code int, resp *http.Response, err error)
Get send a HTTP GET request, no header, just passive nil.
func (*HTTPClient) NewBatchGet ¶
func (s *HTTPClient) NewBatchGet(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)
NewBatchGet new a batch get requests
func (*HTTPClient) NewBatchPost ¶
func (s *HTTPClient) NewBatchPost(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)
NewBatchPost new a batch post requests
func (*HTTPClient) NewTriableGet ¶
func (s *HTTPClient) NewTriableGet(URL string, maxTry int, timeout time.Duration, queryData, header map[string]string) (tr *TriableRequest, err error)
NewTriableGet new a triable request with max retry count
func (*HTTPClient) NewTriablePost ¶
func (s *HTTPClient) NewTriablePost(URL string, maxTry int, timeout time.Duration, data, header map[string]string) (tr *TriableRequest, err error)
NewTriablePost new a triable request with max retry count
func (*HTTPClient) Post ¶
func (s *HTTPClient) Post(u string, data map[string]string, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)
Post send an HTTP POST request, no header, just passive nil. data is form key value.
func (*HTTPClient) PostOfReader ¶
func (s *HTTPClient) PostOfReader(u string, r io.Reader, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)
PostOfReader send a HTTP POST request, no header, just passive nil. data is an io.Reader.
func (*HTTPClient) ProxyUsed ¶
func (s *HTTPClient) ProxyUsed() *url.URL
ProxyUsed returns the final proxy URL used by connect to target
func (*HTTPClient) SetAfterDo ¶
func (s *HTTPClient) SetAfterDo(afterDo AfterDoClientFunc) *HTTPClient
SetAfterDo sets callback call after request sent.
func (*HTTPClient) SetBasicAuth ¶
func (s *HTTPClient) SetBasicAuth(username, password string)
func (*HTTPClient) SetBeforeDo ¶
func (s *HTTPClient) SetBeforeDo(beforeDo BeforeDoClientFunc) *HTTPClient
SetBeforeDo sets callback call before request sent.
func (*HTTPClient) SetClientCert ¶
func (s *HTTPClient) SetClientCert(certPemBytes, keyBytes []byte) (err error)
SetClientCert sets client certificate send to https server, when https server requires client certificate.
func (*HTTPClient) SetConnWrap ¶
SetConnWrap called after net.DialTimeout, you can change the conn,return it, if no change just return it.
func (*HTTPClient) SetDNS ¶
func (s *HTTPClient) SetDNS(dns ...string) (err error)
SetDNS sets a dns for resolve domain in url when send request.
func (*HTTPClient) SetHttpClientFactory ¶
func (s *HTTPClient) SetHttpClientFactory(httpClientFactory func(r *http.Request) *http.Client)
SetHttpClientFactory sets a http.Client factory, the http.Client it's returned, will be used to send the processed http.Request. the argument is the processed http.Request, you can modify it in your any purpose, and the modified http.Request will be sent by the returned client.
func (*HTTPClient) SetKeepalive ¶
func (s *HTTPClient) SetKeepalive(keepalive bool)
func (*HTTPClient) SetPinCert ¶
func (s *HTTPClient) SetPinCert(pemBytes []byte) (err error)
SetPinCert sets https server's certificate.
func (*HTTPClient) SetPreHandler ¶
func (s *HTTPClient) SetPreHandler(preHandler func(r *http.Request))
SetPreHandler handle the processed http.Request, you can modify it in your any purpose, and the modified http.Request will be sent by the returned client.
func (*HTTPClient) SetProxy ¶
func (s *HTTPClient) SetProxy(proxyURL string) (err error)
SetProxy sets the proxies used for send request.
if SetProxyFromEnv sets true and SetProxy sets a proxies url, SetProxy will be working.
func (*HTTPClient) SetProxyFromEnv ¶
func (s *HTTPClient) SetProxyFromEnv(set bool)
SetProxyFromEnv sets if using http_proxy or all_proxy from system environment to send request.
func (*HTTPClient) SetRootCaCerts ¶
func (s *HTTPClient) SetRootCaCerts(caPemBytes ...[]byte) (err error)
SetRootCaCerts sets root certificates to checking https server's certificate.
func (*HTTPClient) Upload ¶
func (s *HTTPClient) Upload(u, fieldName, filename string, data map[string]string, timeout time.Duration, header map[string]string) (body string, resp *http.Response, err error)
Upload uploads a file from a file `filename`, fieldName is the form filed name in form, filename is the value of filed `fieldName` and also the file to upload. data is the additional form data.
func (*HTTPClient) UploadOfReader ¶
func (s *HTTPClient) UploadOfReader(u, fieldName string, filename string, reader io.ReadCloser, data map[string]string, timeout time.Duration, header map[string]string) (body string, resp *http.Response, err error)
UploadOfReader upload a file from a io.Reader `reader`, fieldName is the form filed name in form, filename is the value of filed `fieldName`. data is the additional form data.
type TriableRequest ¶
type TriableRequest struct {
// contains filtered or unexported fields
}
TriableRequest when request fail, retry to request with MaxTryCount.
func NewTriableGet ¶
func NewTriablePost ¶
func NewTriableRequest ¶
func NewTriableRequest(client *http.Client, req *http.Request, maxTry int, timeout time.Duration) *TriableRequest
NewTriableRequest new a TriableRequest by *http.Request, maxTry is the max retry count when a request error occurred. If client is nil, default client will be used.
func NewTriableRequestByURL ¶
func NewTriableRequestByURL(client *http.Client, method string, URL string, maxTry int, timeout time.Duration, data map[string]string, header map[string]string) (tr *TriableRequest, err error)
NewTriableRequestByURL new a TriableRequest by URL.
func (*TriableRequest) AfterDo ¶
func (s *TriableRequest) AfterDo(afterDo AfterDoFunc) *TriableRequest
AfterDo add a callback call after request sent.
func (*TriableRequest) AppendBeforeDo ¶
func (s *TriableRequest) AppendBeforeDo(beforeDo BeforeDoFunc) *TriableRequest
AppendBeforeDo add a callback call before request sent.
func (*TriableRequest) CheckErrorFunc ¶
func (s *TriableRequest) CheckErrorFunc(checkErrorFunc func(idx int, req *http.Request, resp *http.Response) error) *TriableRequest
CheckErrorFunc if returns an error, the request treat as fail.
func (*TriableRequest) Close ¶
func (s *TriableRequest) Close() *TriableRequest
Close close all response body can context cancel func.
func (*TriableRequest) DoFunc ¶
func (s *TriableRequest) DoFunc(doFunc func(req *http.Request) (*http.Response, error)) *TriableRequest
DoFunc sets a request sender.
func (*TriableRequest) Err ¶
func (s *TriableRequest) Err() error
Err returns first error of fail requests.
func (*TriableRequest) ErrAll ¶
func (s *TriableRequest) ErrAll() []error
ErrAll returns all requests error.
func (*TriableRequest) Execute ¶
func (s *TriableRequest) Execute() *Response
Execute send request with retrying ability.
func (*TriableRequest) Keepalive ¶
func (s *TriableRequest) Keepalive(keepalive bool) *TriableRequest
Keepalive sets enable or disable for request keepalive
func (*TriableRequest) SetBeforeDo ¶
func (s *TriableRequest) SetBeforeDo(beforeDo BeforeDoFunc) *TriableRequest
SetBeforeDo sets callback call before request sent.