Documentation
¶
Index ¶
- Variables
- func Get(ctx context.IContext, url string) (resp *http.Response, err error)
- func IsTimeout(err error) bool
- func NewRequest(ctx context.IContext, method, url string, body io.Reader) (*http.Request, error)
- func SetDefaultSetting(setting HttpSettings)
- type HttpClient
- func (c *HttpClient) Do(ctx context.IContext, req *http.Request) (*http.Response, error)
- func (c *HttpClient) Get(ctx context.IContext, url string) (resp *http.Response, err error)
- func (c *HttpClient) GetAs(ctx context.IContext, url string, target interface{}) error
- func (c *HttpClient) GetAsWhenStatusOk(ctx context.IContext, url string, target interface{}) (int, error)
- func (c *HttpClient) GetBytes(ctx context.IContext, url string) ([]byte, error)
- func (c *HttpClient) GetBytesWithStatus(ctx context.IContext, url string) (int, []byte, error)
- func (c *HttpClient) GetJsonWithHeader(ctx context.IContext, urlStr string, header http.Header) (data []byte, err error)
- func (c *HttpClient) GetJsonWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, target interface{}) (err error)
- func (c *HttpClient) GetWithHeader(ctx context.IContext, urlStr string, header http.Header) (data []byte, err error)
- func (c *HttpClient) GetWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, target interface{}) (err error)
- func (c *HttpClient) Post(ctx context.IContext, url string, contentType string, body io.Reader) (resp *http.Response, err error)
- func (c *HttpClient) PostBytes(ctx context.IContext, url string, contentType string, body interface{}) (bytes []byte, err error)
- func (c *HttpClient) PostForm(ctx context.IContext, url string, data url.Values) (resp *http.Response, err error)
- func (c *HttpClient) PostFormAs(ctx context.IContext, url string, v url.Values, target interface{}) error
- func (c *HttpClient) PostFormBytes(ctx context.IContext, urlStr string, v url.Values) (body []byte, err error)
- func (c *HttpClient) PostFormBytesWithStatus(ctx context.IContext, urlStr string, v url.Values) (code int, body []byte, err error)
- func (c *HttpClient) PostFormFileWithHeaderAs(ctx context.IContext, urlStr string, contentType string, header http.Header, ...) (err error)
- func (c *HttpClient) PostFormWithHeader(ctx context.IContext, urlStr string, header http.Header, v url.Values) (data []byte, err error)
- func (c *HttpClient) PostFormWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, v url.Values, ...) (err error)
- func (c *HttpClient) PostJson(ctx context.IContext, urlStr string, content interface{}) (data []byte, err error)
- func (c *HttpClient) PostJsonAs(ctx context.IContext, urlStr string, content interface{}, target interface{}) (err error)
- func (c *HttpClient) PostJsonBytesWithStatus(ctx context.IContext, urlStr string, content interface{}) (code int, body []byte, err error)
- func (c *HttpClient) PostJsonWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, content interface{}, ...) (err error)
- func (c *HttpClient) Put(ctx context.IContext, url string, contentType string, body io.Reader) (resp *http.Response, err error)
- func (c *HttpClient) PutAsJson(ctx context.IContext, urlStr string, content interface{}) (data []byte, err error)
- func (c *HttpClient) PutJsonAs(ctx context.IContext, urlStr string, content interface{}, target interface{}) (err error)
- func (c *HttpClient) RequestJsonWithHeader(ctx context.IContext, urlStr string, method string, header http.Header, ...) (data []byte, err error)
- type HttpClientOption
- type HttpRequest
- func (b *HttpRequest) Body(data interface{}) *HttpRequest
- func (b *HttpRequest) Bytes(ctx gocontext.Context) ([]byte, error)
- func (b *HttpRequest) BytesWithStatus(ctx gocontext.Context) (int, []byte, error)
- func (b *HttpRequest) Debug(debug bool) *HttpRequest
- func (b *HttpRequest) DumpBody(isdump bool) *HttpRequest
- func (b *HttpRequest) Error() error
- func (b *HttpRequest) GetContext() gocontext.Context
- func (b *HttpRequest) GetDumpRequest() []byte
- func (b *HttpRequest) Header(key, value string) *HttpRequest
- func (b *HttpRequest) HeaderNX(key, value string) *HttpRequest
- func (b *HttpRequest) Headers(h http.Header) *HttpRequest
- func (b *HttpRequest) IgnoreResponse(ctx gocontext.Context) error
- func (b *HttpRequest) JSONBody(obj interface{}) *HttpRequest
- func (b *HttpRequest) Param(key, value string) *HttpRequest
- func (b *HttpRequest) Params(kv map[string]string) *HttpRequest
- func (b *HttpRequest) PostFile(formname, filename string) *HttpRequest
- func (b *HttpRequest) Response(ctx gocontext.Context) (*http.Response, error)
- func (b *HttpRequest) SetAcceptEncodingGzip() *HttpRequest
- func (b *HttpRequest) SetAutoGunzip(v bool) *HttpRequest
- func (b *HttpRequest) SetBasicAuth(username, password string) *HttpRequest
- func (b *HttpRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *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(version string) *HttpRequest
- func (b *HttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HttpRequest
- func (b *HttpRequest) SetRequestHook(f func(req *http.Request) error) *HttpRequest
- func (b *HttpRequest) SetRetries(times int) *HttpRequest
- func (b *HttpRequest) SetTLSClientConfig(config *tls.Config) *HttpRequest
- func (b *HttpRequest) SetTimeout(timeout time.Duration) *HttpRequest
- func (b *HttpRequest) SetTransport(transport http.RoundTripper) *HttpRequest
- func (b *HttpRequest) SetUserAgent(useragent string) *HttpRequest
- func (b *HttpRequest) String(ctx gocontext.Context) (string, error)
- func (b *HttpRequest) ToFile(ctx gocontext.Context, filename string) error
- func (b *HttpRequest) ToJSON(ctx gocontext.Context, v interface{}) error
- func (b *HttpRequest) ToJSONWhenStatusOk(ctx gocontext.Context, v interface{}) (code int, err error)
- func (b *HttpRequest) ToJSONWithStatus(ctx gocontext.Context, v interface{}) (code int, err error)
- func (b *HttpRequest) ToXML(ctx gocontext.Context, v interface{}) error
- func (b *HttpRequest) XMLBody(obj interface{}) *HttpRequest
- type HttpSettings
- func (setting *HttpSettings) ContextRequest(ctx gocontext.Context, rawurl, method string) *HttpRequest
- func (setting *HttpSettings) ContextRequestDelete(ctx gocontext.Context, url string) *HttpRequest
- func (setting *HttpSettings) ContextRequestGet(ctx gocontext.Context, url string) *HttpRequest
- func (setting *HttpSettings) ContextRequestHead(ctx gocontext.Context, url string) *HttpRequest
- func (setting *HttpSettings) ContextRequestPatch(ctx gocontext.Context, url string) *HttpRequest
- func (setting *HttpSettings) ContextRequestPost(ctx gocontext.Context, url string) *HttpRequest
- func (setting *HttpSettings) ContextRequestPut(ctx gocontext.Context, url string) *HttpRequest
- func (setting *HttpSettings) Debug(isdebug bool) *HttpSettings
- func (setting *HttpSettings) DumpBody(isdump bool) *HttpSettings
- func (setting *HttpSettings) Request(rawurl, method string) *HttpRequest
- func (setting *HttpSettings) RequestDelete(url string) *HttpRequest
- func (setting *HttpSettings) RequestGet(url string) *HttpRequest
- func (setting *HttpSettings) RequestHead(url string) *HttpRequest
- func (setting *HttpSettings) RequestPatch(url string) *HttpRequest
- func (setting *HttpSettings) RequestPost(url string) *HttpRequest
- func (setting *HttpSettings) RequestPut(url string) *HttpRequest
- func (setting *HttpSettings) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *HttpSettings
- func (setting *HttpSettings) SetDefaultRetries(times int) *HttpSettings
- func (setting *HttpSettings) SetDefaultTimeout(timeout time.Duration) *HttpSettings
- func (setting *HttpSettings) SetEnableCookie(enable bool) *HttpSettings
- func (setting *HttpSettings) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HttpSettings
- func (setting *HttpSettings) SetTLSClientConfig(config *tls.Config) *HttpSettings
- func (setting *HttpSettings) SetTransport(transport http.RoundTripper) *HttpSettings
- func (setting *HttpSettings) SetUserAgent(useragent string) *HttpSettings
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = NewClient()
Functions ¶
func NewRequest ¶
func SetDefaultSetting ¶
func SetDefaultSetting(setting HttpSettings)
SetDefaultSetting Overwrite default settings
Types ¶
type HttpClient ¶
type HttpClient = HttpSettings
func NewClient ¶
func NewClient(opts ...HttpClientOption) *HttpClient
func (*HttpClient) GetAs ¶
func (c *HttpClient) GetAs(ctx context.IContext, url string, target interface{}) error
从url获取返回,并以json格式解析到target
func (*HttpClient) GetAsWhenStatusOk ¶
func (*HttpClient) GetBytesWithStatus ¶
func (*HttpClient) GetJsonWithHeader ¶
func (*HttpClient) GetJsonWithHeaderAs ¶
func (*HttpClient) GetWithHeader ¶
func (*HttpClient) GetWithHeaderAs ¶
func (*HttpClient) Post ¶
func (c *HttpClient) Post(ctx context.IContext, url string, contentType string, body io.Reader) (resp *http.Response, err error)
warning 需要你来将resp.body close
func (*HttpClient) PostForm ¶
func (c *HttpClient) PostForm(ctx context.IContext, url string, data url.Values) (resp *http.Response, err error)
warning 需要你来将resp.body close
func (*HttpClient) PostFormAs ¶
func (*HttpClient) PostFormBytes ¶
func (*HttpClient) PostFormBytesWithStatus ¶
func (*HttpClient) PostFormFileWithHeaderAs ¶
func (*HttpClient) PostFormWithHeader ¶
func (*HttpClient) PostFormWithHeaderAs ¶
func (*HttpClient) PostJsonAs ¶
func (c *HttpClient) PostJsonAs(ctx context.IContext, urlStr string, content interface{}, target interface{}) (err error)
func (*HttpClient) PostJsonBytesWithStatus ¶
func (*HttpClient) PostJsonWithHeaderAs ¶
type HttpClientOption ¶
type HttpClientOption func(*HttpClient)
func WithTimeoutOpt ¶
func WithTimeoutOpt(timeout time.Duration) HttpClientOption
type HttpRequest ¶
type HttpRequest struct {
// contains filtered or unexported fields
}
HttpRequest provides more useful methods for requesting one url than http.Request.
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(ctx gocontext.Context) ([]byte, error)
Bytes returns the body []byte in response. it calls Response inner.
func (*HttpRequest) BytesWithStatus ¶
BytesWithStatus returns the StatusCode in response, and body []byte in response. it calls Response inner.
func (*HttpRequest) Debug ¶
func (b *HttpRequest) Debug(debug bool) *HttpRequest
func (*HttpRequest) DumpBody ¶
func (b *HttpRequest) DumpBody(isdump bool) *HttpRequest
func (*HttpRequest) Error ¶
func (b *HttpRequest) Error() error
func (*HttpRequest) GetContext ¶
func (b *HttpRequest) GetContext() gocontext.Context
func (*HttpRequest) GetDumpRequest ¶
func (b *HttpRequest) GetDumpRequest() []byte
DumpRequest return the DumpRequest
func (*HttpRequest) Header ¶
func (b *HttpRequest) Header(key, value string) *HttpRequest
Header add header item string in request.
func (*HttpRequest) HeaderNX ¶
func (b *HttpRequest) HeaderNX(key, value string) *HttpRequest
func (*HttpRequest) Headers ¶
func (b *HttpRequest) Headers(h http.Header) *HttpRequest
func (*HttpRequest) IgnoreResponse ¶
func (b *HttpRequest) IgnoreResponse(ctx gocontext.Context) error
func (*HttpRequest) JSONBody ¶
func (b *HttpRequest) JSONBody(obj interface{}) *HttpRequest
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) Params ¶
func (b *HttpRequest) Params(kv map[string]string) *HttpRequest
func (*HttpRequest) PostFile ¶
func (b *HttpRequest) PostFile(formname, filename string) *HttpRequest
PostFile add a post file to the request
func (*HttpRequest) SetAcceptEncodingGzip ¶
func (b *HttpRequest) SetAcceptEncodingGzip() *HttpRequest
func (*HttpRequest) SetAutoGunzip ¶
func (b *HttpRequest) SetAutoGunzip(v bool) *HttpRequest
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) SetCheckRedirect ¶
func (b *HttpRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *HttpRequest
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
func (*HttpRequest) SetHost ¶
func (b *HttpRequest) SetHost(host string) *HttpRequest
SetHost set the request host
func (*HttpRequest) SetProtocolVersion ¶
func (b *HttpRequest) SetProtocolVersion(version 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
func (*HttpRequest) SetRequestHook ¶
func (b *HttpRequest) SetRequestHook(f func(req *http.Request) error) *HttpRequest
在真正发起请求之前,允许你对Request对象进行修改
func (*HttpRequest) SetRetries ¶
func (b *HttpRequest) SetRetries(times int) *HttpRequest
Retries sets Retries times. default is 0 means no retried. others means retried times.
func (*HttpRequest) SetTLSClientConfig ¶
func (b *HttpRequest) SetTLSClientConfig(config *tls.Config) *HttpRequest
func (*HttpRequest) SetTimeout ¶
func (b *HttpRequest) SetTimeout(timeout time.Duration) *HttpRequest
request 级的timeout ,只会地当前request 每次请求前都需要设置才生效
func (*HttpRequest) SetTransport ¶
func (b *HttpRequest) SetTransport(transport http.RoundTripper) *HttpRequest
func (*HttpRequest) SetUserAgent ¶
func (b *HttpRequest) SetUserAgent(useragent string) *HttpRequest
SetUserAgent sets User-Agent header field
func (*HttpRequest) String ¶
func (b *HttpRequest) String(ctx gocontext.Context) (string, error)
String returns the body string in response. it calls Response inner.
func (*HttpRequest) ToFile ¶
func (b *HttpRequest) ToFile(ctx gocontext.Context, filename string) error
ToFile saves the body data in response to one file. it calls Response inner.
func (*HttpRequest) ToJSON ¶
func (b *HttpRequest) ToJSON(ctx gocontext.Context, v interface{}) error
ToJSON returns the map that marshals from the body bytes as json in response . it calls Response inner.
func (*HttpRequest) ToJSONWhenStatusOk ¶
func (b *HttpRequest) ToJSONWhenStatusOk(ctx gocontext.Context, v interface{}) (code int, err error)
func (*HttpRequest) ToJSONWithStatus ¶
func (b *HttpRequest) ToJSONWithStatus(ctx gocontext.Context, v interface{}) (code int, err error)
func (*HttpRequest) ToXML ¶
func (b *HttpRequest) ToXML(ctx gocontext.Context, v interface{}) error
ToXML returns the map that marshals from the body bytes as xml in response . it calls Response inner.
func (*HttpRequest) XMLBody ¶
func (b *HttpRequest) XMLBody(obj interface{}) *HttpRequest
XMLBody adds request raw body encoding by XML.
type HttpSettings ¶
type HttpSettings struct {
// contains filtered or unexported fields
}
func NewHttpSettings ¶
func NewHttpSettings() *HttpSettings
func (*HttpSettings) ContextRequest ¶
func (setting *HttpSettings) ContextRequest(ctx gocontext.Context, rawurl, method string) *HttpRequest
func (*HttpSettings) ContextRequestDelete ¶
func (setting *HttpSettings) ContextRequestDelete(ctx gocontext.Context, url string) *HttpRequest
Delete returns *HttpRequest DELETE method.
func (*HttpSettings) ContextRequestGet ¶
func (setting *HttpSettings) ContextRequestGet(ctx gocontext.Context, url string) *HttpRequest
Get returns *HttpRequest with GET method.
func (*HttpSettings) ContextRequestHead ¶
func (setting *HttpSettings) ContextRequestHead(ctx gocontext.Context, url string) *HttpRequest
Head returns *HttpRequest with HEAD method.
func (*HttpSettings) ContextRequestPatch ¶
func (setting *HttpSettings) ContextRequestPatch(ctx gocontext.Context, url string) *HttpRequest
func (*HttpSettings) ContextRequestPost ¶
func (setting *HttpSettings) ContextRequestPost(ctx gocontext.Context, url string) *HttpRequest
Post returns *HttpRequest with POST method.
func (*HttpSettings) ContextRequestPut ¶
func (setting *HttpSettings) ContextRequestPut(ctx gocontext.Context, url string) *HttpRequest
Put returns *HttpRequest with PUT method.
func (*HttpSettings) Debug ¶
func (setting *HttpSettings) Debug(isdebug bool) *HttpSettings
Debug sets show debug or not when executing request.
func (*HttpSettings) DumpBody ¶
func (setting *HttpSettings) DumpBody(isdump bool) *HttpSettings
DumpBody setting whether need to Dump the Body.
func (*HttpSettings) Request ¶
func (setting *HttpSettings) Request(rawurl, method string) *HttpRequest
func (*HttpSettings) RequestDelete ¶
func (setting *HttpSettings) RequestDelete(url string) *HttpRequest
Delete returns *HttpRequest DELETE method.
func (*HttpSettings) RequestGet ¶
func (setting *HttpSettings) RequestGet(url string) *HttpRequest
HttpSettings创建的一个HTTPRequest表示一个http请求, 不要用同一个HttpRequest进行多次请求 Get returns *HttpRequest with GET method.
func (*HttpSettings) RequestHead ¶
func (setting *HttpSettings) RequestHead(url string) *HttpRequest
Head returns *HttpRequest with HEAD method.
func (*HttpSettings) RequestPatch ¶
func (setting *HttpSettings) RequestPatch(url string) *HttpRequest
func (*HttpSettings) RequestPost ¶
func (setting *HttpSettings) RequestPost(url string) *HttpRequest
Post returns *HttpRequest with POST method.
func (*HttpSettings) RequestPut ¶
func (setting *HttpSettings) RequestPut(url string) *HttpRequest
Put returns *HttpRequest with PUT method.
func (*HttpSettings) SetCheckRedirect ¶
func (setting *HttpSettings) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *HttpSettings
SetCheckRedirect specifies the policy for handling redirects.
If checkRedirect is nil, the Client uses its default policy, which is to stop after 10 consecutive requests.
func (*HttpSettings) SetDefaultRetries ¶
func (setting *HttpSettings) SetDefaultRetries(times int) *HttpSettings
Retries sets Retries times. default is 0 means no retried. -1 means retried forever. others means retried times.
func (*HttpSettings) SetDefaultTimeout ¶
func (setting *HttpSettings) SetDefaultTimeout(timeout time.Duration) *HttpSettings
SetDefaultTimeout sets connect time out and read-write time out for Request.
func (*HttpSettings) SetEnableCookie ¶
func (setting *HttpSettings) SetEnableCookie(enable bool) *HttpSettings
SetEnableCookie sets enable/disable cookiejar
func (*HttpSettings) SetProxy ¶
func (setting *HttpSettings) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HttpSettings
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 (*HttpSettings) SetTLSClientConfig ¶
func (setting *HttpSettings) SetTLSClientConfig(config *tls.Config) *HttpSettings
SetTLSClientConfig sets tls connection configurations if visiting https url.
func (*HttpSettings) SetTransport ¶
func (setting *HttpSettings) SetTransport(transport http.RoundTripper) *HttpSettings
SetTransport set the setting transport
func (*HttpSettings) SetUserAgent ¶
func (setting *HttpSettings) SetUserAgent(useragent string) *HttpSettings
SetUserAgent sets User-Agent header field