zhttp

package
v1.7.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 38 Imported by: 14

Documentation

Overview

Package zhttp provides http client related operations

Index

Constants

View Source
const (
	BitReqHead = 1 << iota
	BitReqBody
	BitRespHead
	BitRespBody
	BitTime
	BitStdFlags = BitReqHead | BitReqBody | BitRespHead | BitRespBody
)

Variables

View Source
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")
)
View Source
var (
	Debug = zutil.NewBool(false)
)
View Source
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 Client

func Client() *http.Client

func ConvertCookie added in v1.1.12

func ConvertCookie(cookiesRaw string) map[string]*http.Cookie

ConvertCookie Parse Cookie String

func DisableChunked added in v1.7.19

func DisableChunked(enable ...bool)

func EnableCookie

func EnableCookie(enable bool) error

func EnableInsecureTLS

func EnableInsecureTLS(enable bool)

func File

func File(path string, field ...string) interface{}

func Flags

func Flags() int

func RemoveProxy added in v0.1.26

func RemoveProxy() error

func SetClient

func SetClient(client *http.Client)

func SetFlags

func SetFlags(flags int)

func SetJSONEscapeHTML

func SetJSONEscapeHTML(escape bool)

func SetJSONIndent

func SetJSONIndent(prefix, indent string)

func SetProxy

func SetProxy(proxy func(*http.Request) (*url.URL, error)) error

SetProxy SetProxy

func SetProxyUrl

func SetProxyUrl(proxyUrl ...string) error

SetProxyUrl SetProxyUrl

func SetTimeout

func SetTimeout(d time.Duration)

func SetTransport added in v1.1.12

func SetTransport(transport func(*http.Transport)) error

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 Certificate struct {
	CertFile string
	KeyFile  string
}

type CustomReq added in v1.1.17

type CustomReq func(req *http.Request)

type DownloadProgress

type DownloadProgress func(current, total int64)

type Els added in v1.1.20

type Els []QueryHTML

func (Els) ForEach added in v1.1.20

func (e Els) ForEach(f func(index int, el QueryHTML) bool)

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

func New

func New() *Engine

New create a new Engine

func (*Engine) CheckRedirect added in v0.0.19

func (e *Engine) CheckRedirect(fn ...func(req *http.Request, via []*http.Request) error)

func (*Engine) Client

func (e *Engine) Client() *http.Client

func (*Engine) Connect added in v0.1.64

func (e *Engine) Connect(url string, v ...interface{}) (*Res, error)

func (*Engine) Delete

func (e *Engine) Delete(url string, v ...interface{}) (*Res, error)

func (*Engine) DisableChunked added in v1.7.19

func (e *Engine) DisableChunked(enable ...bool)

func (*Engine) Do

func (e *Engine) Do(method, rawurl string, vs ...interface{}) (resp *Res, err error)

func (*Engine) DoRetry added in v1.1.19

func (e *Engine) DoRetry(attempt int, sleep time.Duration, fn func() (*Res, error)) (res *Res, err error)

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 (e *Engine) EnableCookie(enable bool) error

func (*Engine) EnableInsecureTLS

func (e *Engine) EnableInsecureTLS(enable bool)

func (*Engine) Get

func (e *Engine) Get(url string, v ...interface{}) (*Res, error)

func (*Engine) GetFlags

func (e *Engine) GetFlags() int

func (*Engine) GetRequest added in v1.7.19

func (e *Engine) GetRequest() *Request

GetRequest get request from pool

func (*Engine) Head

func (e *Engine) Head(url string, v ...interface{}) (*Res, error)

func (*Engine) NewRequest added in v1.7.19

func (e *Engine) NewRequest() *Request

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) Options

func (e *Engine) Options(url string, v ...interface{}) (*Res, error)

func (*Engine) Patch

func (e *Engine) Patch(url string, v ...interface{}) (*Res, error)

func (*Engine) Post

func (e *Engine) Post(url string, v ...interface{}) (*Res, error)

func (*Engine) Put

func (e *Engine) Put(url string, v ...interface{}) (*Res, error)

func (*Engine) RemoveProxy added in v0.1.26

func (e *Engine) RemoveProxy() error

func (*Engine) SSE added in v1.4.2

func (e *Engine) SSE(url string, opt func(*SSEOption), v ...interface{}) (*SSEEngine, error)

func (*Engine) SetClient

func (e *Engine) SetClient(client *http.Client)

func (*Engine) SetFlags

func (e *Engine) SetFlags(flags int)

func (*Engine) SetJSONEscapeHTML

func (e *Engine) SetJSONEscapeHTML(escape bool)

func (*Engine) SetJSONIndent

func (e *Engine) SetJSONIndent(prefix, indent string)

func (*Engine) SetProxy

func (e *Engine) SetProxy(proxy func(*http.Request) (*url.URL, error)) error

func (*Engine) SetProxyUrl

func (e *Engine) SetProxyUrl(proxyUrl ...string) error

func (*Engine) SetSsl

func (e *Engine) SetSsl(certPath, keyPath, CAPath string) (*tls.Config, error)

func (*Engine) SetTimeout

func (e *Engine) SetTimeout(d time.Duration)

func (*Engine) SetTransport added in v1.1.12

func (e *Engine) SetTransport(transport func(*http.Transport)) error

func (*Engine) SetUserAgent added in v0.1.42

func (e *Engine) SetUserAgent(fn func() string)

func (*Engine) SetXMLIndent

func (e *Engine) SetXMLIndent(prefix, indent string)

func (*Engine) TlsCertificate added in v1.1.13

func (e *Engine) TlsCertificate(certs ...Certificate) error

func (*Engine) Trace added in v0.1.64

func (e *Engine) Trace(url string, v ...interface{}) (*Res, error)

type FileUpload

type FileUpload struct {
	File      io.ReadCloser
	FileName  string
	FieldName string
}
type Header map[string]string

func RandomUserAgent added in v0.1.37

func RandomUserAgent() Header

type Host

type Host string

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)

func (*JSONRPC) Call added in v1.4.3

func (j *JSONRPC) Call(serviceMethod string, args interface{}, reply interface{}) error

func (*JSONRPC) Close added in v1.4.3

func (j *JSONRPC) Close() error

func (*JSONRPC) Go added in v1.4.3

func (j *JSONRPC) Go(serviceMethod string, args interface{}, reply interface{}, done chan *rpc.Call) *rpc.Call

type JSONRPCOptions added in v1.4.3

type JSONRPCOptions struct {
	TlsConfig  *tls.Config
	Header     http.Header
	Timeout    time.Duration
	RetryDelay time.Duration
	Retry      bool
}

type NoRedirect added in v1.5.2

type NoRedirect bool

type Param

type Param map[string]interface{}

type QueryHTML added in v1.1.1

type QueryHTML struct {
	// contains filtered or unexported fields
}

func HTMLParse added in v1.1.1

func HTMLParse(HTML []byte) (doc QueryHTML, err error)

func (QueryHTML) Attr added in v1.1.1

func (r QueryHTML) Attr(key string) string

func (QueryHTML) Attrs added in v1.1.1

func (r QueryHTML) Attrs() map[string]string

func (QueryHTML) Child deprecated added in v1.1.1

func (r QueryHTML) Child() (childs []QueryHTML)

Deprecated: please use SelectAllChild("") Child All child elements

func (QueryHTML) Exist added in v1.1.1

func (r QueryHTML) Exist() bool

func (QueryHTML) Filter added in v1.6.4

func (r QueryHTML) Filter(el ...QueryHTML) QueryHTML

func (QueryHTML) Find added in v1.1.1

func (r QueryHTML) Find(text string) QueryHTML

func (QueryHTML) ForEachChild added in v1.1.20

func (r QueryHTML) ForEachChild(f func(index int, child QueryHTML) bool)

func (QueryHTML) FullText added in v1.1.1

func (r QueryHTML) FullText(trimSpace ...bool) string

func (QueryHTML) HTML added in v1.1.1

func (r QueryHTML) HTML(trimSpace ...bool) string

func (QueryHTML) Name added in v1.1.1

func (r QueryHTML) Name() string

func (QueryHTML) NthChild added in v1.1.12

func (r QueryHTML) NthChild(index int) QueryHTML

func (QueryHTML) Select added in v1.1.1

func (r QueryHTML) Select(el string, args ...map[string]string) QueryHTML

func (QueryHTML) SelectAll added in v1.1.1

func (r QueryHTML) SelectAll(el string, args ...map[string]string) (arr Els)

func (QueryHTML) SelectAllChild added in v1.1.20

func (r QueryHTML) SelectAllChild(el string, args ...map[string]string) (arr Els)

func (QueryHTML) SelectBrother added in v1.1.12

func (r QueryHTML) SelectBrother(el string, args ...map[string]string) QueryHTML

func (QueryHTML) SelectChild added in v1.1.1

func (r QueryHTML) SelectChild(el string, args ...map[string]string) QueryHTML

func (QueryHTML) SelectParent added in v1.1.1

func (r QueryHTML) SelectParent(el string, args ...map[string]string) QueryHTML

func (QueryHTML) String added in v1.1.20

func (r QueryHTML) String() string

func (QueryHTML) Text added in v1.1.1

func (r QueryHTML) Text(trimSpace ...bool) string

type QueryParam

type QueryParam map[string]interface{}

type Request added in v1.7.19

type Request struct {
	// contains filtered or unexported fields
}

func GetRequest added in v1.7.19

func GetRequest() *Request

GetRequest get request from pool

func NewRequest added in v1.7.19

func NewRequest() *Request

NewRequest create new request

func (*Request) Body added in v1.7.19

func (r *Request) Body(body interface{}) *Request

Body set request body

func (*Request) Client added in v1.7.19

func (r *Request) Client(client *http.Client) *Request

Client set http client

func (*Request) Context added in v1.7.19

func (r *Request) Context(ctx context.Context) *Request

Context set context

func (*Request) Cookie added in v1.7.19

func (r *Request) Cookie(cookie *http.Cookie) *Request

Cookie set cookie

func (*Request) Custom added in v1.7.19

func (r *Request) Custom(fn CustomReq) *Request

Custom custom request handler

func (*Request) DELETE added in v1.7.19

func (r *Request) DELETE() (*Res, error)

DELETE do delete request

func (*Request) Do added in v1.7.19

func (r *Request) Do() (*Res, error)

Do do request

func (*Request) DoAndRelease added in v1.7.19

func (r *Request) DoAndRelease() (*Res, error)

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) Form added in v1.7.19

func (r *Request) Form(key string, value interface{}) *Request

Form set form param

func (*Request) FormMap added in v1.7.19

func (r *Request) FormMap(params Param) *Request

FormMap set form params

func (*Request) GET added in v1.7.19

func (r *Request) GET() (*Res, error)

GET do get request

func (*Request) GetAndRelease added in v1.7.19

func (r *Request) GetAndRelease() (*Res, error)

GetAndRelease do get request and release to pool

func (*Request) HEAD added in v1.7.19

func (r *Request) HEAD() (*Res, error)

HEAD do head request

func (*Request) Header added in v1.7.19

func (r *Request) Header(key, value string) *Request

Header set request header

func (*Request) Headers added in v1.7.19

func (r *Request) Headers(headers Header) *Request

Headers set request headers

func (*Request) Host added in v1.7.19

func (r *Request) Host(host string) *Request

Host set host

func (*Request) JSON added in v1.7.19

func (r *Request) JSON(v interface{}) *Request

JSON set json request body

func (*Request) Method added in v1.7.19

func (r *Request) Method(method string) *Request

Method set request method

func (*Request) NoRedirect added in v1.7.19

func (r *Request) NoRedirect(disable bool) *Request

NoRedirect disable redirect

func (*Request) OPTIONS added in v1.7.19

func (r *Request) OPTIONS() (*Res, error)

OPTIONS do options request

func (*Request) PATCH added in v1.7.19

func (r *Request) PATCH() (*Res, error)

PATCH do patch request

func (*Request) POST added in v1.7.19

func (r *Request) POST() (*Res, error)

POST do post request

func (*Request) PUT added in v1.7.19

func (r *Request) PUT() (*Res, error)

PUT do put request

func (*Request) PostAndRelease added in v1.7.19

func (r *Request) PostAndRelease() (*Res, error)

PostAndRelease do post request and release to pool

func (*Request) Query added in v1.7.19

func (r *Request) Query(key string, value interface{}) *Request

Query set query param

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) Reset added in v1.7.19

func (r *Request) Reset() *Request

Reset reset request

func (*Request) URL added in v1.7.19

func (r *Request) URL(url string) *Request

URL set request url

func (*Request) UploadProgress added in v1.7.19

func (r *Request) UploadProgress(progress UploadProgress) *Request

UploadProgress set upload progress callback

func (*Request) XML added in v1.7.19

func (r *Request) XML(v interface{}) *Request

XML set xml request body

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 Connect added in v0.1.64

func Connect(url string, v ...interface{}) (*Res, error)

func Delete

func Delete(url string, v ...interface{}) (*Res, error)

func Do

func Do(method, rawurl string, v ...interface{}) (resp *Res, err error)

func DoRetry added in v1.1.19

func DoRetry(attempt int, sleep time.Duration, fn func() (*Res, error)) (*Res, error)

func DoWithArgs added in v1.7.19

func DoWithArgs(method, rawurl string, args *RequestArgs) (*Res, error)

DoWithArgs use struct args to do request

func Get

func Get(url string, v ...interface{}) (*Res, error)
func Head(url string, v ...interface{}) (*Res, error)

func Options

func Options(url string, v ...interface{}) (*Res, error)

func Patch

func Patch(url string, v ...interface{}) (*Res, error)

func Post

func Post(url string, v ...interface{}) (*Res, error)

func Put

func Put(url string, v ...interface{}) (*Res, error)

func Trace added in v0.1.64

func Trace(url string, v ...interface{}) (*Res, error)

func (*Res) Body

func (r *Res) Body() (body io.ReadCloser)

func (*Res) Bytes

func (r *Res) Bytes() []byte

func (*Res) Cost

func (r *Res) Cost() time.Duration

func (*Res) Dump

func (r *Res) Dump() string

func (*Res) GetCookie

func (r *Res) GetCookie() map[string]*http.Cookie

func (*Res) HTML added in v1.1.1

func (r *Res) HTML() (doc QueryHTML)

func (*Res) JSON added in v1.1.1

func (r *Res) JSON(key string) *zjson.Res

func (*Res) JSONs added in v1.1.3

func (r *Res) JSONs() *zjson.Res

func (*Res) Request

func (r *Res) Request() *http.Request

func (*Res) Response

func (r *Res) Response() *http.Response

func (*Res) StatusCode added in v0.1.58

func (r *Res) StatusCode() int

func (*Res) Stream added in v1.4.2

func (r *Res) Stream(fn func(line []byte, eof bool) error) error

func (*Res) String

func (r *Res) String() string

func (*Res) ToBytes

func (r *Res) ToBytes() ([]byte, error)

func (*Res) ToFile

func (r *Res) ToFile(name string) error

func (*Res) ToJSON

func (r *Res) ToJSON(v interface{}) error

func (*Res) ToString

func (r *Res) ToString() (string, error)

func (*Res) ToXML

func (r *Res) ToXML(v interface{}) error

func (Res) Upload

func (m Res) Upload(req *http.Request)

func (Res) UploadChunke added in v0.1.58

func (m Res) UploadChunke(req *http.Request)

type SSEEngine added in v1.4.2

type SSEEngine struct {
	// contains filtered or unexported fields
}

func SSE added in v1.4.2

func SSE(url string, v ...interface{}) (*SSEEngine, error)

func (*SSEEngine) Close added in v1.4.2

func (sse *SSEEngine) Close()

func (*SSEEngine) Done added in v1.4.2

func (sse *SSEEngine) Done() <-chan struct{}

func (*SSEEngine) Error added in v1.5.2

func (sse *SSEEngine) Error() <-chan error

func (*SSEEngine) Event added in v1.4.2

func (sse *SSEEngine) Event() <-chan *SSEEvent

func (*SSEEngine) OnMessage added in v1.5.2

func (sse *SSEEngine) OnMessage(fn func(*SSEEvent)) (<-chan struct{}, error)

func (*SSEEngine) VerifyHeader added in v1.6.2

func (sse *SSEEngine) VerifyHeader(fn func(http.Header) bool)

type SSEEvent added in v1.4.2

type SSEEvent struct {
	ID        string
	Event     string
	Undefined []byte
	Data      []byte
}

type SSEOption added in v1.6.4

type SSEOption struct {
	Method   string
	RetryNum int
}

type UploadProgress

type UploadProgress func(current, total int64)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL