Documentation ¶
Index ¶
- Constants
- Variables
- func AnyToString(i interface{}) string
- func IsTimeout(err interface{}) bool
- type Decoder
- type Logger
- type ZClient
- func NewClient(timeout time.Duration, flags int, logger Logger) *ZClient
- func NewClientWithHTTPClient(hc *http.Client, flags int, logger Logger) *ZClient
- func NewClientWithSSLSkipVerify(timeout time.Duration, flags int, logger Logger) *ZClient
- func NewClientWithTransport(timeout time.Duration, ts *http.Transport, flags int, logger Logger) *ZClient
- type ZRequest
- func (zr *ZRequest) Delete(urlStr string) *ZRequest
- func (zr *ZRequest) DisableAtUpload() *ZRequest
- func (zr *ZRequest) Do(method, urlStr string) *ZRequest
- func (zr *ZRequest) Duration() time.Duration
- func (zr *ZRequest) EnableAtUpload() *ZRequest
- func (zr *ZRequest) Get(urlStr string) *ZRequest
- func (zr *ZRequest) GetBodyBuf() io.Reader
- func (zr *ZRequest) GetHeader() http.Header
- func (zr *ZRequest) GetURLStr() string
- func (zr *ZRequest) Head(urlStr string) *ZRequest
- func (zr *ZRequest) Options(urlStr string) *ZRequest
- func (zr *ZRequest) Patch(urlStr string) *ZRequest
- func (zr *ZRequest) Post(urlStr string) *ZRequest
- func (zr *ZRequest) Put(urlStr string) *ZRequest
- func (zr *ZRequest) Resp() (*http.Response, error)
- func (zr *ZRequest) RespBody() ([]byte, error)
- func (zr *ZRequest) RespBodyN(n int64) ([]byte, error)
- func (zr *ZRequest) RespBodyString() (string, error)
- func (zr *ZRequest) RespBodyStringN(n int64) (string, error)
- func (zr *ZRequest) RespCookies() []*http.Cookie
- func (zr *ZRequest) RespHeader(key string) string
- func (zr *ZRequest) RespHeaders() http.Header
- func (zr *ZRequest) RespStatus() string
- func (zr *ZRequest) RespStatusCode() int
- func (zr *ZRequest) SetBasicAuth(username, password string) *ZRequest
- func (zr *ZRequest) SetBody(body interface{}) *ZRequest
- func (zr *ZRequest) SetBodyBuf(buf io.Reader) *ZRequest
- func (zr *ZRequest) SetContentType(contentType string) *ZRequest
- func (zr *ZRequest) SetCookie(ck *http.Cookie) *ZRequest
- func (zr *ZRequest) SetCookieString(str string) *ZRequest
- func (zr *ZRequest) SetCookies(cks []*http.Cookie) *ZRequest
- func (zr *ZRequest) SetHeader(key, value string) *ZRequest
- func (zr *ZRequest) SetHeaders(headers map[string]string) *ZRequest
- func (zr *ZRequest) SetQueryParam(key, value string) *ZRequest
- func (zr *ZRequest) SetQueryParamAny(key string, value interface{}) *ZRequest
- func (zr *ZRequest) SetQueryParams(params map[string]string) *ZRequest
- func (zr *ZRequest) SetQueryParamsAny(params map[string]interface{}) *ZRequest
- func (zr *ZRequest) SetURLStr(urlStr string) *ZRequest
- func (zr *ZRequest) SetUserAgent(ua string) *ZRequest
- func (zr *ZRequest) Unmarshal(v interface{}) error
Constants ¶
const ( // GET HTTP method GET = "GET" // POST HTTP method POST = "POST" // PUT HTTP method PUT = "PUT" // DELETE HTTP method DELETE = "DELETE" // PATCH HTTP method PATCH = "PATCH" // HEAD HTTP method HEAD = "HEAD" // OPTIONS HTTP method OPTIONS = "OPTIONS" )
const ( // FlagLogOn determine log on/off FlagLogOn = 1 << iota // FlagLogDetail determine log type (detail or summary) FlagLogDetail // FlagLogBody determine log request/response body // httputil dumpbody into memory // So, be careful of the large body request/response FlagLogBody )
Variables ¶
var ( HdrUserAgent = http.CanonicalHeaderKey("User-Agent") HdrContentType = http.CanonicalHeaderKey("Content-Type") FormBoundary = "FormBoundarykKyzkULVDem6riojjQMsLa2tgA" PlainTextType = "text/plain; charset=utf-8" JSONContentType = "application/json; charset=utf-8" FormContentType = "application/x-www-form-urlencoded" MultipartContentType = "multipart/form-data; boundary=" + FormBoundary StreamContentType = "application/octet-stream" ErrRequestNotComp = errors.New("resp is nil, request not completed") )
var ( // TransportWithSSLSkipVerify is the default transport which not auth the ssl server TransportWithSSLSkipVerify = &http.Transport{ Proxy: http.ProxyFromEnvironment, Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } // DefaultLogger is a logger default used DefaultLogger = log.New(os.Stderr, "[zrequest]", 0) )
Functions ¶
func IsTimeout ¶
func IsTimeout(err interface{}) bool
IsTimeout go 1.6 http://stackoverflow.com/questions/23494950/specifically-check-for-timeout-error
Types ¶
type Logger ¶
type Logger interface { Println(v ...interface{}) Printf(format string, v ...interface{}) }
Logger requests log
type ZClient ¶
type ZClient struct {
// contains filtered or unexported fields
}
ZClient wrapped with http client
func NewClientWithHTTPClient ¶
NewClientWithHTTPClient wrapped with a exist http client
func NewClientWithSSLSkipVerify ¶
NewClientWithSSLSkipVerify client not auth ssl server
type ZRequest ¶
type ZRequest struct { BeforeHookFunc func(*ZRequest) error // contains filtered or unexported fields }
ZRequest this struct used for a single-http-roundtrip
func (*ZRequest) DisableAtUpload ¶
DisableAtUpload enable '@' prefix to upload a file, default is false
func (*ZRequest) EnableAtUpload ¶
EnableAtUpload enable '@' prefix to upload a file, default is false
func (*ZRequest) GetBodyBuf ¶
GetBodyBuf get the request bodybuf
func (*ZRequest) Resp ¶
Resp get raw response , the response.Body is not closed You should close the response body
func (*ZRequest) RespBodyString ¶
RespBodyString response body as string
func (*ZRequest) RespBodyStringN ¶
RespBodyStringN read response body as string with max n bytes
func (*ZRequest) RespCookies ¶
RespCookies response cookies
func (*ZRequest) RespHeader ¶
RespHeader response header
func (*ZRequest) RespHeaders ¶
RespHeaders response headers
func (*ZRequest) RespStatus ¶
RespStatus response status text
func (*ZRequest) RespStatusCode ¶
RespStatusCode response status code
func (*ZRequest) SetBasicAuth ¶
SetBasicAuth set a 401 authentication (replace)
func (*ZRequest) SetBodyBuf ¶
SetBodyBuf set the request bodybuf
func (*ZRequest) SetContentType ¶
SetContentType set the request Content-Type
func (*ZRequest) SetCookieString ¶
SetCookieString set request cookies with given str
func (*ZRequest) SetCookies ¶
SetCookies set request cookies
func (*ZRequest) SetHeaders ¶
SetHeaders set multi request header(replace)
func (*ZRequest) SetQueryParam ¶
SetQueryParam set a query param (replace)
func (*ZRequest) SetQueryParamAny ¶
SetQueryParamAny set a query params with any type
func (*ZRequest) SetQueryParams ¶
SetQueryParams set query params (replace)
func (*ZRequest) SetQueryParamsAny ¶
SetQueryParamsAny set query params with any type
func (*ZRequest) SetUserAgent ¶
SetUserAgent set the request ua