http

package
v0.0.0-...-9c17c29 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ContentTypeJson = "application/json;charset=UTF-8"
	ContentTypeForm = "application/x-www-form-urlencoded"
)

Variables

View Source
var (
	MarshalErr        = fmt.Errorf("marhsal request failed")
	NewHttpRequestErr = fmt.Errorf("new httpRequest failed")
	HttpRequestErr    = fmt.Errorf("http request failed")
	UnmarshalErr      = fmt.Errorf("unmaral response failed")
	UnGzipErr         = fmt.Errorf("ungzip failed")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Timeout               uint  `yaml:"timeout" default:"20"`                 //请求总的超时时间, 单位:s
	MaxIdleConns          uint  `yaml:"max_idle_conns" default:"1000"`        //最大的空闲连接数,单位:s
	MaxIdleConnsPerHost   uint  `yaml:"max_idle_conns_per_host" default:"30"` //单个url的最大空闲连接数,单位:s
	ConnectTimeout        uint  `yaml:"connect_timeout" default:"30"`         //建立连接超时时间,单位:s
	KeepAliveInterval     uint  `yaml:"keep_alive_interval" default:"30"`     //存活探测间隔时间,单位:s
	IdleConnTimeout       uint  `yaml:"idle_conn_timeout" default:"500"`      //连接的最大空闲时间,单位:s
	TLSHandshakeTimeout   uint  `yaml:"tls_handshake_timeout" default:"5"`    //执行TLS握手的超时时间,单位:s
	ExpectContinueTimeout uint  `yaml:"expect_continue_timeout"`              //写Header与写Body之间,等待服务端报头的超时时间
	ResponseHeaderTimeout uint  `yaml:"response_header_timeout"`              //响应包头的最大超时时间
	DisableRetry          bool  `yaml:"disable_retry"`                        //错误不重试
	DisablePool           bool  `yaml:"disable_pool"`                         //禁用连接池意思是只使用短连接
	Verbose               *bool `yaml:"verbose" default:"false"`              //是否打印请求日志
}

type Hook

type Hook interface {
	BeforeRequest(context.Context, *http.Request) (context.Context, error)
	AfterRequest(context.Context, *http.Request, *http.Response, error) error
}

type HttpClient

type HttpClient interface {
	Get(requestId, url string, params map[string][]string, response *Response, header map[string]string) error
	GetJson(requestId, url string, request interface{}, response *Response, header map[string]string) error
	PostJson(requestId, url string, request interface{}, response *Response, header map[string]string) error
	PostForm(requestId, url string, form map[string]interface{}, response *Response, header map[string]string) error
	Put(requestId, url string, request interface{}, response *Response, header map[string]string) error
	Patch(requestId, url string, request interface{}, response *Response, header map[string]string) error
	Delete(requestId, url string, request interface{}, response *Response, header map[string]string) error
	SetRequestIdCallBack(func(requestId string, header map[string]string))
	AddHook(hook Hook)
	Do(method, requestId, url, contentType string, request interface{}, values url.Values, response *Response, header map[string]string) (err error)
}

func NewHttpClient

func NewHttpClient(config Config) HttpClient

type Response

type Response struct {
	StatusCode int
	Data       interface{}
}

Jump to

Keyboard shortcuts

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