Documentation
¶
Overview ¶
Package httpclient 是 net/http 的轻量封装:ctx-first、JSON 便捷方法、 简单重试,并自动透传 igo context 的 meta header(含 traceId)到下游服务。
基本用法:
client := httpclient.New(httpclient.WithTimeout(3*time.Second)) var out SomeResp err := client.PostJSON(ctx, url, req, &out)
简单请求可直接用包级默认客户端:
resp, err := httpclient.Get(ctx, url)
Index ¶
- Variables
- func GetBytes(ctx context.Context, url string, opts ...ReqOption) ([]byte, error)
- func GetJSON(ctx context.Context, url string, out any, opts ...ReqOption) error
- func PostJSON(ctx context.Context, url string, in, out any, opts ...ReqOption) error
- type Client
- func (c *Client) Do(ctx context.Context, method, rawurl string, body io.Reader, header http.Header) (*Response, error)
- func (c *Client) Get(ctx context.Context, url string, opts ...ReqOption) (*Response, error)
- func (c *Client) GetBytes(ctx context.Context, url string, opts ...ReqOption) ([]byte, error)
- func (c *Client) GetJSON(ctx context.Context, url string, out any, opts ...ReqOption) error
- func (c *Client) Post(ctx context.Context, url string, contentType string, body io.Reader, ...) (*Response, error)
- func (c *Client) PostForm(ctx context.Context, url string, form url.Values, opts ...ReqOption) (*Response, error)
- func (c *Client) PostFormJSON(ctx context.Context, url string, form url.Values, out any, opts ...ReqOption) error
- func (c *Client) PostJSON(ctx context.Context, url string, in any, out any, opts ...ReqOption) error
- func (c *Client) PutJSON(ctx context.Context, url string, in any, out any, opts ...ReqOption) error
- type Option
- func WithDebug(b bool) Option
- func WithHeader(key, value string) Option
- func WithInsecureSkipVerify() Option
- func WithProxyFunc(f func(*http.Request) (*url.URL, error)) Option
- func WithProxyURL(rawurl string) Option
- func WithRetries(n int) Option
- func WithTimeout(d time.Duration) Option
- func WithTransport(rt http.RoundTripper) Option
- func WithUserAgent(ua string) Option
- type ReqOption
- type Response
Constants ¶
This section is empty.
Variables ¶
var Default = New()
Default 包级默认客户端(10s 超时)
Functions ¶
Types ¶
type Client ¶ added in v0.2.0
type Client struct {
// contains filtered or unexported fields
}
Client HTTP 客户端,并发安全,建议复用(内部连接池)
func (*Client) Do ¶ added in v0.2.0
func (c *Client) Do(ctx context.Context, method, rawurl string, body io.Reader, header http.Header) (*Response, error)
Do 发起请求并读取完整响应。body 会被完整缓冲以支持重试。 header 参数为本次请求的附加 header,可为 nil。
func (*Client) Post ¶ added in v0.2.0
func (c *Client) Post(ctx context.Context, url string, contentType string, body io.Reader, opts ...ReqOption) (*Response, error)
Post 发起 POST 请求
func (*Client) PostForm ¶ added in v0.2.0
func (c *Client) PostForm(ctx context.Context, url string, form url.Values, opts ...ReqOption) (*Response, error)
PostForm 发起表单 POST 请求
func (*Client) PostFormJSON ¶ added in v0.4.1
func (c *Client) PostFormJSON(ctx context.Context, url string, form url.Values, out any, opts ...ReqOption) error
PostFormJSON 发起表单 POST 请求并把 JSON 响应反序列化到 out(out 可为 nil)
type Option ¶ added in v0.4.0
type Option func(*Client)
Option 客户端配置项
func WithHeader ¶ added in v0.4.0
WithHeader 设置每个请求都会附带的默认 header
func WithInsecureSkipVerify ¶ added in v0.4.1
func WithInsecureSkipVerify() Option
WithInsecureSkipVerify 跳过 TLS 证书校验(自签名证书/抓包调试场景,生产慎用)
func WithProxyFunc ¶ added in v0.4.1
WithProxyFunc 设置动态代理函数(每次请求调用,适合代理地址从配置热读取的场景)
func WithProxyURL ¶ added in v0.4.1
WithProxyURL 设置固定代理地址,如 "http://127.0.0.1:7890"
func WithRetries ¶ added in v0.4.0
WithRetries 设置网络层错误(连接失败、超时等)的重试次数,HTTP 状态码错误不重试
func WithTimeout ¶ added in v0.4.0
WithTimeout 设置整体请求超时(默认 10s)
func WithTransport ¶ added in v0.4.0
func WithTransport(rt http.RoundTripper) Option
WithTransport 自定义 Transport(完全接管;与 WithProxy*/WithInsecureSkipVerify 同用时请放在它们之前)
func WithUserAgent ¶ added in v0.4.0
WithUserAgent 设置 User-Agent
type ReqOption ¶ added in v0.4.1
ReqOption 单次请求的配置项(目前用于附加 header)
func WithReqHeader ¶ added in v0.4.1
WithReqHeader 为本次请求附加一个 header
func WithReqHeaders ¶ added in v0.4.1
WithReqHeaders 为本次请求附加一组 header(map[string][]string 可直接传入)