httpclient

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var Default = New()

Default 包级默认客户端(10s 超时)

Functions

func GetBytes added in v0.4.1

func GetBytes(ctx context.Context, url string, opts ...ReqOption) ([]byte, error)

GetBytes 使用默认客户端发起 GET 请求并返回响应 body

func GetJSON added in v0.4.0

func GetJSON(ctx context.Context, url string, out any, opts ...ReqOption) error

GetJSON 使用默认客户端发起 GET 请求并解析 JSON 响应

func PostJSON added in v0.4.0

func PostJSON(ctx context.Context, url string, in, out any, opts ...ReqOption) error

PostJSON 使用默认客户端发起 JSON POST 请求

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(opts ...Option) *Client

New 创建客户端(默认 10s 超时、合理的连接池配置)

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) Get added in v0.2.0

func (c *Client) Get(ctx context.Context, url string, opts ...ReqOption) (*Response, error)

Get 发起 GET 请求

func (*Client) GetBytes added in v0.4.1

func (c *Client) GetBytes(ctx context.Context, url string, opts ...ReqOption) ([]byte, error)

GetBytes 发起 GET 请求并返回响应 body(非 2xx 返回错误),适合下载文件/原始内容

func (*Client) GetJSON added in v0.2.0

func (c *Client) GetJSON(ctx context.Context, url string, out any, opts ...ReqOption) error

GetJSON 发起 GET 请求并把 JSON 响应反序列化到 out(out 可为 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)

func (*Client) PostJSON added in v0.2.0

func (c *Client) PostJSON(ctx context.Context, url string, in any, out any, opts ...ReqOption) error

PostJSON 发起 JSON POST 请求并把 JSON 响应反序列化到 out(out 可为 nil)

func (*Client) PutJSON added in v0.4.0

func (c *Client) PutJSON(ctx context.Context, url string, in any, out any, opts ...ReqOption) error

PutJSON 发起 JSON PUT 请求并把 JSON 响应反序列化到 out(out 可为 nil)

type Option added in v0.4.0

type Option func(*Client)

Option 客户端配置项

func WithDebug added in v0.4.0

func WithDebug(b bool) Option

WithDebug 开启请求/响应日志

func WithHeader added in v0.4.0

func WithHeader(key, value string) Option

WithHeader 设置每个请求都会附带的默认 header

func WithInsecureSkipVerify added in v0.4.1

func WithInsecureSkipVerify() Option

WithInsecureSkipVerify 跳过 TLS 证书校验(自签名证书/抓包调试场景,生产慎用)

func WithProxyFunc added in v0.4.1

func WithProxyFunc(f func(*http.Request) (*url.URL, error)) Option

WithProxyFunc 设置动态代理函数(每次请求调用,适合代理地址从配置热读取的场景)

func WithProxyURL added in v0.4.1

func WithProxyURL(rawurl string) Option

WithProxyURL 设置固定代理地址,如 "http://127.0.0.1:7890"

func WithRetries added in v0.4.0

func WithRetries(n int) Option

WithRetries 设置网络层错误(连接失败、超时等)的重试次数,HTTP 状态码错误不重试

func WithTimeout added in v0.4.0

func WithTimeout(d time.Duration) Option

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

func WithUserAgent(ua string) Option

WithUserAgent 设置 User-Agent

type ReqOption added in v0.4.1

type ReqOption func(http.Header)

ReqOption 单次请求的配置项(目前用于附加 header)

func WithReqHeader added in v0.4.1

func WithReqHeader(key, value string) ReqOption

WithReqHeader 为本次请求附加一个 header

func WithReqHeaders added in v0.4.1

func WithReqHeaders(headers http.Header) ReqOption

WithReqHeaders 为本次请求附加一组 header(map[string][]string 可直接传入)

type Response added in v0.2.0

type Response struct {
	StatusCode int
	Header     http.Header
	Body       []byte
}

Response HTTP 响应(body 已全部读入内存)

func Get

func Get(ctx context.Context, url string, opts ...ReqOption) (*Response, error)

Get 使用默认客户端发起 GET 请求

func (*Response) JSON added in v0.2.0

func (r *Response) JSON(v any) error

JSON 反序列化响应体

func (*Response) OK added in v0.4.0

func (r *Response) OK() bool

OK 状态码是否为 2xx

func (*Response) String added in v0.2.0

func (r *Response) String() string

String 响应体字符串

Jump to

Keyboard shortcuts

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