Documentation
¶
Overview ¶
Package zhttp provides http client related operations
Index ¶
- Constants
- Variables
- func BodyJSON(v interface{}) *bodyJson
- func BodyXML(v interface{}) *bodyXml
- func Client() *http.Client
- func ConvertCookie(cookiesRaw string) map[string]*http.Cookie
- func DisableChunked(enable ...bool)
- func EnableCookie(enable bool) error
- func EnableInsecureTLS(enable bool)
- func File(path string, field ...string) interface{}
- func Flags() int
- func RemoveProxy() error
- func SetClient(client *http.Client)
- func SetFlags(flags int)
- func SetJSONEscapeHTML(escape bool)
- func SetJSONIndent(prefix, indent string)
- func SetProxy(proxy func(*http.Request) (*url.URL, error)) error
- func SetProxyUrl(proxyUrl ...string) error
- func SetTimeout(d time.Duration)
- func SetTransport(transport func(*http.Transport)) error
- func SetUserAgent(fn func() string)
- func SetXMLIndent(prefix, indent string)
- func TlsCertificate(certs ...Certificate) error
- type Certificate
- type CustomReq
- type DownloadProgress
- type Els
- type Engine
- func (e *Engine) CheckRedirect(fn ...func(req *http.Request, via []*http.Request) error)
- func (e *Engine) Client() *http.Client
- func (e *Engine) Connect(url string, v ...interface{}) (*Res, error)
- func (e *Engine) Delete(url string, v ...interface{}) (*Res, error)
- func (e *Engine) DisableChunked(enable ...bool)
- func (e *Engine) Do(method, rawurl string, vs ...interface{}) (resp *Res, err error)
- func (e *Engine) DoRetry(attempt int, sleep time.Duration, fn func() (*Res, error)) (res *Res, err error)
- func (e *Engine) DoWithArgs(method, rawurl string, args *RequestArgs) (*Res, error)
- func (e *Engine) EnableCookie(enable bool) error
- func (e *Engine) EnableInsecureTLS(enable bool)
- func (e *Engine) Get(url string, v ...interface{}) (*Res, error)
- func (e *Engine) GetFlags() int
- func (e *Engine) GetRequest() *Request
- func (e *Engine) Head(url string, v ...interface{}) (*Res, error)
- func (e *Engine) NewRequest() *Request
- func (e *Engine) OptimizeForHighConcurrency()
- func (e *Engine) OptimizeForLowLatency()
- func (e *Engine) Options(url string, v ...interface{}) (*Res, error)
- func (e *Engine) Patch(url string, v ...interface{}) (*Res, error)
- func (e *Engine) Post(url string, v ...interface{}) (*Res, error)
- func (e *Engine) Put(url string, v ...interface{}) (*Res, error)
- func (e *Engine) RemoveProxy() error
- func (e *Engine) SSE(url string, opt func(*SSEOption), v ...interface{}) (*SSEEngine, error)
- func (e *Engine) SetClient(client *http.Client)
- func (e *Engine) SetFlags(flags int)
- func (e *Engine) SetJSONEscapeHTML(escape bool)
- func (e *Engine) SetJSONIndent(prefix, indent string)
- func (e *Engine) SetProxy(proxy func(*http.Request) (*url.URL, error)) error
- func (e *Engine) SetProxyUrl(proxyUrl ...string) error
- func (e *Engine) SetSsl(certPath, keyPath, CAPath string) (*tls.Config, error)
- func (e *Engine) SetTimeout(d time.Duration)
- func (e *Engine) SetTransport(transport func(*http.Transport)) error
- func (e *Engine) SetUserAgent(fn func() string)
- func (e *Engine) SetXMLIndent(prefix, indent string)
- func (e *Engine) TlsCertificate(certs ...Certificate) error
- func (e *Engine) Trace(url string, v ...interface{}) (*Res, error)
- type FileUpload
- type Header
- type Host
- type JSONRPC
- type JSONRPCOptions
- type NoRedirect
- type Param
- type QueryHTML
- func (r QueryHTML) Attr(key string) string
- func (r QueryHTML) Attrs() map[string]string
- func (r QueryHTML) Child() (childs []QueryHTML)deprecated
- func (r QueryHTML) Exist() bool
- func (r QueryHTML) Filter(el ...QueryHTML) QueryHTML
- func (r QueryHTML) Find(text string) QueryHTML
- func (r QueryHTML) ForEachChild(f func(index int, child QueryHTML) bool)
- func (r QueryHTML) FullText(trimSpace ...bool) string
- func (r QueryHTML) HTML(trimSpace ...bool) string
- func (r QueryHTML) Name() string
- func (r QueryHTML) NthChild(index int) QueryHTML
- func (r QueryHTML) Select(el string, args ...map[string]string) QueryHTML
- func (r QueryHTML) SelectAll(el string, args ...map[string]string) (arr Els)
- func (r QueryHTML) SelectAllChild(el string, args ...map[string]string) (arr Els)
- func (r QueryHTML) SelectBrother(el string, args ...map[string]string) QueryHTML
- func (r QueryHTML) SelectChild(el string, args ...map[string]string) QueryHTML
- func (r QueryHTML) SelectParent(el string, args ...map[string]string) QueryHTML
- func (r QueryHTML) String() string
- func (r QueryHTML) Text(trimSpace ...bool) string
- type QueryParam
- type Request
- func (r *Request) Body(body interface{}) *Request
- func (r *Request) Client(client *http.Client) *Request
- func (r *Request) Context(ctx context.Context) *Request
- func (r *Request) Cookie(cookie *http.Cookie) *Request
- func (r *Request) Custom(fn CustomReq) *Request
- func (r *Request) DELETE() (*Res, error)
- func (r *Request) Do() (*Res, error)
- func (r *Request) DoAndRelease() (*Res, error)
- func (r *Request) DownloadProgress(progress DownloadProgress) *Request
- func (r *Request) File(fieldName, fileName string, file io.ReadCloser) *Request
- func (r *Request) Form(key string, value interface{}) *Request
- func (r *Request) FormMap(params Param) *Request
- func (r *Request) GET() (*Res, error)
- func (r *Request) GetAndRelease() (*Res, error)
- func (r *Request) HEAD() (*Res, error)
- func (r *Request) Header(key, value string) *Request
- func (r *Request) Headers(headers Header) *Request
- func (r *Request) Host(host string) *Request
- func (r *Request) JSON(v interface{}) *Request
- func (r *Request) Method(method string) *Request
- func (r *Request) NoRedirect(disable bool) *Request
- func (r *Request) OPTIONS() (*Res, error)
- func (r *Request) PATCH() (*Res, error)
- func (r *Request) POST() (*Res, error)
- func (r *Request) PUT() (*Res, error)
- func (r *Request) PostAndRelease() (*Res, error)
- func (r *Request) Query(key string, value interface{}) *Request
- func (r *Request) QueryMap(params QueryParam) *Request
- func (r *Request) Release()
- func (r *Request) Reset() *Request
- func (r *Request) URL(url string) *Request
- func (r *Request) UploadProgress(progress UploadProgress) *Request
- func (r *Request) XML(v interface{}) *Request
- type RequestArgs
- type Res
- func Connect(url string, v ...interface{}) (*Res, error)
- func Delete(url string, v ...interface{}) (*Res, error)
- func Do(method, rawurl string, v ...interface{}) (resp *Res, err error)
- func DoRetry(attempt int, sleep time.Duration, fn func() (*Res, error)) (*Res, error)
- func DoWithArgs(method, rawurl string, args *RequestArgs) (*Res, error)
- func Get(url string, v ...interface{}) (*Res, error)
- func Head(url string, v ...interface{}) (*Res, error)
- func Options(url string, v ...interface{}) (*Res, error)
- func Patch(url string, v ...interface{}) (*Res, error)
- func Post(url string, v ...interface{}) (*Res, error)
- func Put(url string, v ...interface{}) (*Res, error)
- func Trace(url string, v ...interface{}) (*Res, error)
- func (r *Res) Body() (body io.ReadCloser)
- func (r *Res) Bytes() []byte
- func (r *Res) Cost() time.Duration
- func (r *Res) Dump() string
- func (r *Res) GetCookie() map[string]*http.Cookie
- func (r *Res) HTML() (doc QueryHTML)
- func (r *Res) JSON(key string) *zjson.Res
- func (r *Res) JSONs() *zjson.Res
- func (r *Res) Request() *http.Request
- func (r *Res) Response() *http.Response
- func (r *Res) StatusCode() int
- func (r *Res) Stream(fn func(line []byte, eof bool) error) error
- func (r *Res) String() string
- func (r *Res) ToBytes() ([]byte, error)
- func (r *Res) ToFile(name string) error
- func (r *Res) ToJSON(v interface{}) error
- func (r *Res) ToString() (string, error)
- func (r *Res) ToXML(v interface{}) error
- func (m Res) Upload(req *http.Request)
- func (m Res) UploadChunke(req *http.Request)
- type SSEEngine
- func (sse *SSEEngine) Close()
- func (sse *SSEEngine) Done() <-chan struct{}
- func (sse *SSEEngine) Error() <-chan error
- func (sse *SSEEngine) Event() <-chan *SSEEvent
- func (sse *SSEEngine) OnMessage(fn func(*SSEEvent)) (<-chan struct{}, error)
- func (sse *SSEEngine) VerifyHeader(fn func(http.Header) bool)
- type SSEEvent
- type SSEOption
- type UploadProgress
Constants ¶
const ( BitReqHead = 1 << iota BitReqBody BitRespHead BitRespBody BitTime BitStdFlags = BitReqHead | BitReqBody | BitRespHead | BitRespBody )
Variables ¶
var ( ErrNoTransport = errors.New("no transport") ErrUrlNotSpecified = errors.New("url not specified") ErrTransEmpty = errors.New("trans is empty") ErrNoMatched = errors.New("no file have been matched") )
var (
Debug = zutil.NewBool(false)
)
var UserAgentLists = []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
"Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255",
"Mozilla/5.0 (X11; OpenBSD i386) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 5.1) Gecko/20100101 Firefox/14.0 Opera/12.0",
"Mozilla/5.0 (compatible; Googlebot/2.1;+http://www.google.com/bot.html)",
}
Functions ¶
func BodyJSON ¶
func BodyJSON(v interface{}) *bodyJson
BodyJSON make the object be encoded in json format and set it to the request body
func BodyXML ¶
func BodyXML(v interface{}) *bodyXml
BodyXML make the object be encoded in xml format and set it to the request body
func ConvertCookie ¶ added in v1.1.12
ConvertCookie Parse Cookie String
func DisableChunked ¶ added in v1.7.19
func DisableChunked(enable ...bool)
func EnableCookie ¶
func EnableInsecureTLS ¶
func EnableInsecureTLS(enable bool)
func RemoveProxy ¶ added in v0.1.26
func RemoveProxy() error
func SetJSONEscapeHTML ¶
func SetJSONEscapeHTML(escape bool)
func SetJSONIndent ¶
func SetJSONIndent(prefix, indent string)
func SetTimeout ¶
func SetTransport ¶ added in v1.1.12
SetTransport SetTransport
func SetUserAgent ¶ added in v0.1.42
func SetUserAgent(fn func() string)
SetUserAgent returning an empty array means random built-in User Agent
func SetXMLIndent ¶
func SetXMLIndent(prefix, indent string)
func TlsCertificate ¶ added in v1.1.13
func TlsCertificate(certs ...Certificate) error
Types ¶
type Certificate ¶ added in v1.1.13
type DownloadProgress ¶
type DownloadProgress func(current, total int64)
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) CheckRedirect ¶ added in v0.0.19
func (*Engine) DisableChunked ¶ added in v1.7.19
func (*Engine) DoWithArgs ¶ added in v1.7.19
func (e *Engine) DoWithArgs(method, rawurl string, args *RequestArgs) (*Res, error)
DoWithArgs use struct args to do request
func (*Engine) EnableCookie ¶
func (*Engine) EnableInsecureTLS ¶
func (*Engine) GetRequest ¶ added in v1.7.19
GetRequest get request from pool
func (*Engine) NewRequest ¶ added in v1.7.19
NewRequest create new request
func (*Engine) OptimizeForHighConcurrency ¶ added in v1.7.19
func (e *Engine) OptimizeForHighConcurrency()
OptimizeForHighConcurrency High concurrency optimization
func (*Engine) OptimizeForLowLatency ¶ added in v1.7.19
func (e *Engine) OptimizeForLowLatency()
OptimizeForLowLatency Low latency optimization
func (*Engine) RemoveProxy ¶ added in v0.1.26
func (*Engine) SetJSONEscapeHTML ¶
func (*Engine) SetJSONIndent ¶
func (*Engine) SetProxyUrl ¶
func (*Engine) SetTimeout ¶
func (*Engine) SetTransport ¶ added in v1.1.12
func (*Engine) SetUserAgent ¶ added in v0.1.42
func (*Engine) SetXMLIndent ¶
func (*Engine) TlsCertificate ¶ added in v1.1.13
func (e *Engine) TlsCertificate(certs ...Certificate) error
type FileUpload ¶
type FileUpload struct {
File io.ReadCloser
FileName string
FieldName string
}
type Header ¶
func RandomUserAgent ¶ added in v0.1.37
func RandomUserAgent() Header
type JSONRPC ¶ added in v1.4.3
type JSONRPC struct {
// contains filtered or unexported fields
}
func NewJSONRPC ¶ added in v1.4.3
func NewJSONRPC(address string, path string, opts ...func(o *JSONRPCOptions)) (client *JSONRPC, err error)
type JSONRPCOptions ¶ added in v1.4.3
type NoRedirect ¶ added in v1.5.2
type NoRedirect bool
type QueryHTML ¶ added in v1.1.1
type QueryHTML struct {
// contains filtered or unexported fields
}
func (QueryHTML) ForEachChild ¶ added in v1.1.20
func (QueryHTML) SelectAllChild ¶ added in v1.1.20
func (QueryHTML) SelectBrother ¶ added in v1.1.12
func (QueryHTML) SelectChild ¶ added in v1.1.1
func (QueryHTML) SelectParent ¶ added in v1.1.1
type QueryParam ¶
type QueryParam map[string]interface{}
type Request ¶ added in v1.7.19
type Request struct {
// contains filtered or unexported fields
}
func (*Request) DoAndRelease ¶ added in v1.7.19
DoAndRelease do request and release to pool
func (*Request) DownloadProgress ¶ added in v1.7.19
func (r *Request) DownloadProgress(progress DownloadProgress) *Request
DownloadProgress set download progress callback
func (*Request) File ¶ added in v1.7.19
func (r *Request) File(fieldName, fileName string, file io.ReadCloser) *Request
File set file upload
func (*Request) GetAndRelease ¶ added in v1.7.19
GetAndRelease do get request and release to pool
func (*Request) NoRedirect ¶ added in v1.7.19
NoRedirect disable redirect
func (*Request) PostAndRelease ¶ added in v1.7.19
PostAndRelease do post request and release to pool
func (*Request) QueryMap ¶ added in v1.7.19
func (r *Request) QueryMap(params QueryParam) *Request
QueryMap set query params
func (*Request) Release ¶ added in v1.7.19
func (r *Request) Release()
Release release request to pool
func (*Request) UploadProgress ¶ added in v1.7.19
func (r *Request) UploadProgress(progress UploadProgress) *Request
UploadProgress set upload progress callback
type RequestArgs ¶ added in v1.7.19
type RequestArgs struct {
Body interface{}
Ctx context.Context
UploadProg UploadProgress
FormParams Param
Client *http.Client
QueryParams QueryParam
Headers Header
DownloadProg DownloadProgress
CustomReq CustomReq
Host string
Uploads []FileUpload
Cookies []*http.Cookie
NoRedirect bool
}
type Res ¶
type Res struct {
// contains filtered or unexported fields
}
func DoWithArgs ¶ added in v1.7.19
func DoWithArgs(method, rawurl string, args *RequestArgs) (*Res, error)
DoWithArgs use struct args to do request
func (*Res) Body ¶
func (r *Res) Body() (body io.ReadCloser)
func (*Res) StatusCode ¶ added in v0.1.58
func (Res) UploadChunke ¶ added in v0.1.58
type SSEEngine ¶ added in v1.4.2
type SSEEngine struct {
// contains filtered or unexported fields
}
type UploadProgress ¶
type UploadProgress func(current, total int64)