httpclient

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = NewClient()

Functions

func Get

func Get(ctx context.IContext, url string) (resp *http.Response, err error)

warning 需要你来将resp.body close

func IsTimeout

func IsTimeout(err error) bool

func NewRequest

func NewRequest(ctx context.IContext, method, url string, body io.Reader) (*http.Request, error)

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

func (c *HttpClient) Do(ctx context.IContext, req *http.Request) (*http.Response, error)

func (*HttpClient) Get

func (c *HttpClient) Get(ctx context.IContext, url string) (resp *http.Response, err error)

warning 需要你来将resp.body close

func (*HttpClient) GetAs

func (c *HttpClient) GetAs(ctx context.IContext, url string, target interface{}) error

从url获取返回,并以json格式解析到target

func (*HttpClient) GetAsWhenStatusOk

func (c *HttpClient) GetAsWhenStatusOk(ctx context.IContext, url string, target interface{}) (int, error)

func (*HttpClient) GetBytes

func (c *HttpClient) GetBytes(ctx context.IContext, url string) ([]byte, error)

func (*HttpClient) GetBytesWithStatus

func (c *HttpClient) GetBytesWithStatus(ctx context.IContext, url string) (int, []byte, error)

func (*HttpClient) GetJsonWithHeader

func (c *HttpClient) GetJsonWithHeader(ctx context.IContext, urlStr string, header http.Header) (data []byte, err error)

func (*HttpClient) GetJsonWithHeaderAs

func (c *HttpClient) GetJsonWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, target interface{}) (err error)

func (*HttpClient) GetWithHeader

func (c *HttpClient) GetWithHeader(ctx context.IContext, urlStr string, header http.Header) (data []byte, err error)

func (*HttpClient) GetWithHeaderAs

func (c *HttpClient) GetWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, target interface{}) (err error)

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

func (c *HttpClient) PostBytes(ctx context.IContext, url string, contentType string, body interface{}) (bytes []byte, err error)

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 (c *HttpClient) PostFormAs(ctx context.IContext, url string, v url.Values, target interface{}) error

func (*HttpClient) PostFormBytes

func (c *HttpClient) PostFormBytes(ctx context.IContext, urlStr string, v url.Values) (body []byte, err error)

func (*HttpClient) PostFormBytesWithStatus

func (c *HttpClient) PostFormBytesWithStatus(ctx context.IContext, urlStr string, v url.Values) (code int, body []byte, err error)

func (*HttpClient) PostFormFileWithHeaderAs

func (c *HttpClient) PostFormFileWithHeaderAs(ctx context.IContext, urlStr string, contentType string, header http.Header, v interface{}, target interface{}) (err error)

func (*HttpClient) PostFormWithHeader

func (c *HttpClient) PostFormWithHeader(ctx context.IContext, urlStr string, header http.Header, v url.Values) (data []byte, err error)

func (*HttpClient) PostFormWithHeaderAs

func (c *HttpClient) PostFormWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, v url.Values, target interface{}) (err error)

func (*HttpClient) PostJson

func (c *HttpClient) PostJson(ctx context.IContext, urlStr string, content interface{}) (data []byte, err error)

func (*HttpClient) PostJsonAs

func (c *HttpClient) PostJsonAs(ctx context.IContext, urlStr string, content interface{}, target interface{}) (err error)

func (*HttpClient) PostJsonBytesWithStatus

func (c *HttpClient) PostJsonBytesWithStatus(ctx context.IContext, urlStr string, content interface{}) (code int, body []byte, err error)

func (*HttpClient) PostJsonWithHeaderAs

func (c *HttpClient) PostJsonWithHeaderAs(ctx context.IContext, urlStr string, header http.Header, content interface{}, target interface{}) (err error)

func (*HttpClient) Put

func (c *HttpClient) Put(ctx context.IContext, url string, contentType string, body io.Reader) (resp *http.Response, err error)

func (*HttpClient) PutAsJson

func (c *HttpClient) PutAsJson(ctx context.IContext, urlStr string, content interface{}) (data []byte, err error)

func (*HttpClient) PutJsonAs

func (c *HttpClient) PutJsonAs(ctx context.IContext, urlStr string, content interface{}, target interface{}) (err error)

func (*HttpClient) RequestJsonWithHeader

func (c *HttpClient) RequestJsonWithHeader(ctx context.IContext, urlStr string, method string, header http.Header, content interface{}) (data []byte, err error)

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

func (b *HttpRequest) BytesWithStatus(ctx gocontext.Context) (int, []byte, error)

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

func (b *HttpRequest) Response(ctx gocontext.Context) (*http.Response, error)

Response executes request client gets response mannually.

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

Jump to

Keyboard shortcuts

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