Documentation
¶
Overview ¶
Package httpclient 提供通用 HTTP 请求工具(零依赖 net/http): GET/POST/PUT/DELETE,支持 query 参数、表单、JSON、原始 body、自定义 header。 与 framework/thirdparty 的区别:thirdparty 面向签名对接,本包面向通用 HTTP 调用。
Index ¶
- func SetGlobal(client *Client)
- type Client
- func (c *Client) Delete(ctx context.Context, path string, options Options, out interface{}) (*Response, error)
- func (c *Client) Get(ctx context.Context, path string, options Options, out interface{}) (*Response, error)
- func (c *Client) Post(ctx context.Context, path string, options Options, out interface{}) (*Response, error)
- func (c *Client) Put(ctx context.Context, path string, options Options, out interface{}) (*Response, error)
- type Config
- type Options
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client HTTP 客户端(并发安全)。
func (*Client) Delete ¶
func (c *Client) Delete(ctx context.Context, path string, options Options, out interface{}) (*Response, error)
Delete DELETE 请求。
func (*Client) Get ¶
func (c *Client) Get(ctx context.Context, path string, options Options, out interface{}) (*Response, error)
Get GET 请求;out 非 nil 时自动 JSON 解码到 out。
type Config ¶
type Config struct {
// BaseURL 基础地址(可选,path 为绝对 URL 时忽略)。
BaseURL string
// Timeout 请求超时(默认 10s)。
Timeout time.Duration
// DefaultHeaders 默认请求头(每个请求都带,可被 Options.Headers 覆盖)。
DefaultHeaders map[string]string
// MaxRetries 网络错误重试次数(默认 0 不重试)。
MaxRetries int
}
Config 客户端配置。
type Options ¶
type Options struct {
// Query 查询参数(URL 编码拼接)。
Query map[string]string
// Headers 请求头(合并 DefaultHeaders,同名覆盖)。
Headers map[string]string
// Form 表单参数(application/x-www-form-urlencoded)。
Form map[string]string
// JSON 对象(application/json 序列化)。
JSON interface{}
// Body 原始请求体(与 ContentType 配合;优先级高于 Form/JSON)。
Body []byte
// ContentType 请求体类型(如 text/plain、application/xml)。
ContentType string
}
Options 单次请求选项。
Click to show internal directories.
Click to hide internal directories.