httpclient

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v0.2.0

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

Client 简化的HTTP客户端

func New added in v0.2.0

func New() *Client

New 创建新的HTTP客户端

func NewWithTimeout added in v0.2.0

func NewWithTimeout(timeout time.Duration) *Client

NewWithTimeout 创建带超时的HTTP客户端

func (*Client) AddHeader added in v0.2.0

func (c *Client) AddHeader(key, value string) *Client

AddHeader 添加默认请求头(不覆盖已存在的)

func (*Client) Debug added in v0.2.0

func (c *Client) Debug(debug bool) *Client

Debug 设置调试模式

func (*Client) Delete added in v0.2.0

func (c *Client) Delete(ctx ictx.Context, url string) (*Response, error)

Delete 请求

func (*Client) Do added in v0.2.0

func (c *Client) Do(ctx ictx.Context, req *Request) (*Response, error)

Do 执行HTTP请求(核心方法,不包含重试)

func (*Client) DoWithRetry added in v0.2.0

func (c *Client) DoWithRetry(ctx ictx.Context, req *Request) (*Response, error)

DoWithRetry 执行HTTP请求(带重试功能)

func (*Client) Get added in v0.2.0

func (c *Client) Get(ctx ictx.Context, url string) (*Response, error)

Get 请求

func (*Client) GetBodyString added in v0.2.0

func (c *Client) GetBodyString(ctx ictx.Context, method, url string, body interface{}) (string, error)

GetBodyString 获取响应体字符串的便捷方法(支持任何HTTP方法)

func (*Client) GetJSON added in v0.2.0

func (c *Client) GetJSON(ctx ictx.Context, url string, target interface{}) error

GetJSON 获取JSON数据的便捷方法

func (*Client) Post added in v0.2.0

func (c *Client) Post(ctx ictx.Context, url string, body interface{}) (*Response, error)

Post 请求

func (*Client) PostForm added in v0.2.0

func (c *Client) PostForm(ctx ictx.Context, url string, data url.Values) (*Response, error)

PostForm 发送表单数据

func (*Client) PostJSON added in v0.2.0

func (c *Client) PostJSON(ctx ictx.Context, url string, data interface{}, target interface{}) error

PostJSON 发送JSON数据的便捷方法

func (*Client) Put added in v0.2.0

func (c *Client) Put(ctx ictx.Context, url string, body interface{}) (*Response, error)

Put 请求

func (*Client) SetBaseURL added in v0.2.0

func (c *Client) SetBaseURL(baseURL string) *Client

SetBaseURL 设置基础URL

func (*Client) SetDefaultRetries added in v0.2.0

func (c *Client) SetDefaultRetries(maxRetries int) *Client

SetDefaultRetries 设置默认重试次数

func (*Client) SetDefaultTimeout added in v0.2.0

func (c *Client) SetDefaultTimeout(timeout time.Duration) *Client

SetDefaultTimeout 设置默认超时时间

func (*Client) SetHeader added in v0.2.0

func (c *Client) SetHeader(key, value string) *Client

SetHeader 设置默认请求头

func (*Client) SetHeaders added in v0.2.0

func (c *Client) SetHeaders(headers map[string]string) *Client

SetHeaders 批量设置默认请求头

func (*Client) SetTLSClientConfig added in v0.2.0

func (c *Client) SetTLSClientConfig(config *tls.Config) *Client

SetTLSClientConfig 设置TLS配置

func (*Client) SetUserAgent added in v0.2.0

func (c *Client) SetUserAgent(userAgent string) *Client

SetUserAgent 设置User-Agent

type Request added in v0.2.0

type Request struct {
	Method string
	URL    string
	Body   io.Reader
	Header http.Header
}

Request HTTP请求结构

type Response added in v0.2.0

type Response struct {
	*http.Response
	Body []byte
}

Response HTTP响应结构

func (*Response) JSON added in v0.2.0

func (r *Response) JSON(target interface{}) error

JSON 解析响应为JSON

func (*Response) String added in v0.2.0

func (r *Response) String() string

String 获取响应字符串

type RetryConfig added in v0.2.0

type RetryConfig struct {
	MaxRetries  int           // 最大重试次数
	InitialWait time.Duration // 初始等待时间
	MaxWait     time.Duration // 最大等待时间
	BackoffRate float64       // 退避倍率
}

RetryConfig 重试配置

Jump to

Keyboard shortcuts

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