Help us shape the future of Go! Take the 2026 Go Developer Survey

httpcli

package
v1.12.9 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete[T any](ctx context.Context, url string, opts ...RequestOption) (*T, error)

Delete 便捷 DELETE 请求(全局单例)

func Do

func Do(ctx context.Context, method, url string, reqBody interface{}, opts ...RequestOption) ([]byte, error)

Do 便捷通用请求(全局单例)

func Get

func Get[T any](ctx context.Context, url string, opts ...RequestOption) (*T, error)

Get 便捷 GET 请求(全局单例)

func GetRealIP

func GetRealIP(r *http.Request) string

getRealIP 从 HTTP 请求中提取客户端真实 IP 地址 优先级:X-Forwarded-For > X-Real-IP > RemoteAddr

func Post

func Post[T any](ctx context.Context, url string, reqBody interface{}, opts ...RequestOption) (*T, error)

Post 便捷 POST 请求(全局单例)

func Put

func Put[T any](ctx context.Context, url string, reqBody interface{}, opts ...RequestOption) (*T, error)

Put 便捷 PUT 请求(全局单例)

func SetObject

func SetObject(oj *HttpClient)

Types

type Component

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

func (*Component) Close

func (d *Component) Close()

func (*Component) Init

func (d *Component) Init(data map[string]any) error

type Config

type Config struct {
	Timeout             int64 `yaml:"timeout,omitempty"`                 // 总超时时间(秒,推荐 5-10s)
	MaxIdleConns        int   `yaml:"max_idle_conns,omitempty"`          // 最大空闲连接数
	MaxIdleConnsPerHost int   `yaml:"max_idle_conns_per_host,omitempty"` // 每个Host最大空闲连接
	IdleConnTimeout     int64 `yaml:"idle_conn_timeout,omitempty"`       // 空闲连接超时(秒)
}

type HttpClient

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

func NewHttpClient

func NewHttpClient() *HttpClient

func (*HttpClient) Close

func (c *HttpClient) Close()

func (*HttpClient) Do

func (c *HttpClient) Do(ctx context.Context, method, url string, reqBody interface{}, opts ...RequestOption) ([]byte, error)

Do 核心请求方法

func (*HttpClient) Init

func (c *HttpClient) Init(cfg *Config) error

type RequestOption

type RequestOption func(r *http.Request)

func WithContext

func WithContext(ctx context.Context) RequestOption

func WithHeader

func WithHeader(key, value string) RequestOption

Jump to

Keyboard shortcuts

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