http

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const GET = "GET"
View Source
const POST = "POST"

Variables

This section is empty.

Functions

func SetDefaultSetting

func SetDefaultSetting(setting ZngueHttpSettings)

SetDefaultSetting overwrites default settings

func TimeoutDialer

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)

TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.

Types

type Filter

type Filter func(ctx context.Context, req *ZngueHttp) (*http.Response, error)

type FilterChain

type FilterChain func(next Filter) Filter

type HttpFn

type HttpFn func(http *ZngueHttp) *ZngueHttp

type HttpMico

type HttpMico struct {
	Method    string
	Url       string
	Host      string
	EndPoint  string
	ServiceId string
	Body      []byte
	Param     map[string]interface{}
	Header    map[string]interface{}

	Timeout time.Duration
	// contains filtered or unexported fields
}

func (*HttpMico) Bind

func (m *HttpMico) Bind(i interface{}) error

读取并绑定数据

func (*HttpMico) BuildUrl

func (m *HttpMico) BuildUrl() (string, error)

创建url

func (*HttpMico) DoRequest

func (m *HttpMico) DoRequest() (*ZngueHttp, error)

func (*HttpMico) Formaturl

func (m *HttpMico) Formaturl(url string) string

func (*HttpMico) GetLocal

func (m *HttpMico) GetLocal() (string, error)

本地数据请求

func (*HttpMico) GetResiger

func (m *HttpMico) GetResiger() (string, error)

redis数据读取

func (*HttpMico) Response

func (m *HttpMico) Response() (string, error)

type ZngueHttp

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

ZngueHttp provides more useful methods than http.Request for requesting a url.

func Delete

func Delete(url string) *ZngueHttp

Delete returns *ZngueHttp DELETE method.

func Get

func Get(url string) *ZngueHttp

Get returns *ZngueHttp with GET method.

func Head(url string) *ZngueHttp

Head returns *ZngueHttp with HEAD method.

func NewZngueRequest

func NewZngueRequest(rawurl, method string) *ZngueHttp

NewZngueRequest returns *ZngueHttp with specific method

func Post

func Post(url string) *ZngueHttp

Post returns *ZngueHttp with POST method.

func Put

func Put(url string) *ZngueHttp

Put returns *ZngueHttp with PUT method.

func (*ZngueHttp) AddFilters

func (b *ZngueHttp) AddFilters(fcs ...FilterChain) *ZngueHttp

AddFilters adds filter

func (*ZngueHttp) Body

func (b *ZngueHttp) Body(data interface{}) *ZngueHttp

Body adds request raw body. Supports string and []byte.

func (*ZngueHttp) Bytes

func (b *ZngueHttp) Bytes() ([]byte, error)

Bytes returns the body []byte in response. Calls Response inner.

func (*ZngueHttp) Debug

func (b *ZngueHttp) Debug(isdebug bool) *ZngueHttp

Debug sets show debug or not when executing request.

func (*ZngueHttp) DoRequest

func (b *ZngueHttp) DoRequest() (resp *http.Response, err error)

DoRequest executes client.Do

func (*ZngueHttp) DoRequestWithCtx

func (b *ZngueHttp) DoRequestWithCtx(ctx context.Context) (resp *http.Response, err error)

func (*ZngueHttp) DumpBody

func (b *ZngueHttp) DumpBody(isdump bool) *ZngueHttp

DumpBody sets the DumbBody field

func (*ZngueHttp) DumpRequest

func (b *ZngueHttp) DumpRequest() []byte

DumpRequest returns the DumpRequest

func (*ZngueHttp) GetRequest

func (b *ZngueHttp) GetRequest() *http.Request

GetRequest returns the request object

func (*ZngueHttp) Header

func (b *ZngueHttp) Header(key, value string) *ZngueHttp

Header adds header item string in request.

func (*ZngueHttp) JSONBody

func (b *ZngueHttp) JSONBody(obj interface{}) (*ZngueHttp, error)

JSONBody adds the request raw body encoded in JSON.

func (*ZngueHttp) Param

func (b *ZngueHttp) Param(key, value string) *ZngueHttp

Param adds query param in to request. params build query string as ?key1=value1&key2=value2...

func (*ZngueHttp) PostFile

func (b *ZngueHttp) PostFile(formname, filename string) *ZngueHttp

PostFile adds a post file to the request

func (*ZngueHttp) Response

func (b *ZngueHttp) Response() (*http.Response, error)

Response executes request client gets response manually.

func (*ZngueHttp) Retries

func (b *ZngueHttp) Retries(times int) *ZngueHttp

Retries sets Retries times. default is 0 (never retry) -1 retry indefinitely (forever) Other numbers specify the exact retry amount

func (*ZngueHttp) RetryDelay

func (b *ZngueHttp) RetryDelay(delay time.Duration) *ZngueHttp

RetryDelay sets the time to sleep between reconnection attempts

func (*ZngueHttp) SetBasicAuth

func (b *ZngueHttp) SetBasicAuth(username, password string) *ZngueHttp

SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.

func (*ZngueHttp) SetCheckRedirect

func (b *ZngueHttp) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *ZngueHttp

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 (*ZngueHttp) SetCookie

func (b *ZngueHttp) SetCookie(cookie *http.Cookie) *ZngueHttp

SetCookie adds a cookie to the request.

func (*ZngueHttp) SetEnableCookie

func (b *ZngueHttp) SetEnableCookie(enable bool) *ZngueHttp

SetEnableCookie sets enable/disable cookiejar

func (*ZngueHttp) SetFilters

func (b *ZngueHttp) SetFilters(fcs ...FilterChain) *ZngueHttp

SetFilters will use the filter as the invocation filters

func (*ZngueHttp) SetHost

func (b *ZngueHttp) SetHost(host string) *ZngueHttp

SetHost set the request host

func (*ZngueHttp) SetProtocolVersion

func (b *ZngueHttp) SetProtocolVersion(vers string) *ZngueHttp

SetProtocolVersion sets the protocol version for incoming requests. Client requests always use HTTP/1.1.

func (*ZngueHttp) SetProxy

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

SetProxy sets the HTTP proxy example:

func(req *http.Request) (*url.URL, error) {
	u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
	return u, nil
}

func (*ZngueHttp) SetTLSClientConfig

func (b *ZngueHttp) SetTLSClientConfig(config *tls.Config) *ZngueHttp

SetTLSClientConfig sets TLS connection configuration if visiting HTTPS url.

func (*ZngueHttp) SetTimeout

func (b *ZngueHttp) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *ZngueHttp

SetTimeout sets connect time out and read-write time out for ZngueRequest.

func (*ZngueHttp) SetTransport

func (b *ZngueHttp) SetTransport(transport http.RoundTripper) *ZngueHttp

SetTransport sets the transport field

func (*ZngueHttp) SetUserAgent

func (b *ZngueHttp) SetUserAgent(useragent string) *ZngueHttp

SetUserAgent sets User-Agent header field

func (*ZngueHttp) Setting

func (b *ZngueHttp) Setting(setting ZngueHttpSettings) *ZngueHttp

Setting changes request settings

func (*ZngueHttp) String

func (b *ZngueHttp) String() (string, error)

String returns the body string in response. Calls Response inner.

func (*ZngueHttp) ToFile

func (b *ZngueHttp) ToFile(filename string) error

ToFile saves the body data in response to one file. Calls Response inner.

func (*ZngueHttp) ToJSON

func (b *ZngueHttp) ToJSON(v interface{}) error

ToJSON returns the map that marshals from the body bytes as json in response. Calls Response inner.

func (*ZngueHttp) ToXML

func (b *ZngueHttp) ToXML(v interface{}) error

ToXML returns the map that marshals from the body bytes as xml in response . Calls Response inner.

func (*ZngueHttp) ToYAML

func (b *ZngueHttp) ToYAML(v interface{}) error

ToYAML returns the map that marshals from the body bytes as yaml in response . Calls Response inner.

func (*ZngueHttp) XMLBody

func (b *ZngueHttp) XMLBody(obj interface{}) (*ZngueHttp, error)

XMLBody adds the request raw body encoded in XML.

func (*ZngueHttp) YAMLBody

func (b *ZngueHttp) YAMLBody(obj interface{}) (*ZngueHttp, error)

YAMLBody adds the request raw body encoded in YAML.

type ZngueHttpSettings

type ZngueHttpSettings struct {
	ShowDebug        bool
	UserAgent        string
	ConnectTimeout   time.Duration
	ReadWriteTimeout time.Duration
	TLSClientConfig  *tls.Config
	Proxy            func(*http.Request) (*url.URL, error)
	Transport        http.RoundTripper
	CheckRedirect    func(req *http.Request, via []*http.Request) error
	EnableCookie     bool
	Gzip             bool
	DumpBody         bool
	Retries          int // if set to -1 means will retry forever
	RetryDelay       time.Duration
	FilterChains     []FilterChain
}

ZngueHttpSettings is the http.Client setting

Jump to

Keyboard shortcuts

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