API 参考
HTTPC 提供 32 个请求选项函数、5 个配置预设、7 个内置中间件和完整的下载支持。
核心架构
HTTPC 采用双层设计:Layer 1 的方法 API 是轻量封装,实际处理请求的是 Layer 2 的 Handler 管线。
text
HTTPC 双层架构
├── Layer 1 方法 API(轻量封装)
│ 包级函数 httpc.Get/Post/... + Client 方法 + 请求选项 → Result
│
└── Layer 2 Handler 管线(请求处理引擎)
MiddlewareFunc(Handler) 洋葱链
→ 组装 clientImpl.middlewareChain
→ 执行(每个请求 = 组装并执行一条 Handler 链)模块导航
核心
| 模块 | 说明 |
|---|---|
| 包级函数与客户端方法 | Get/Post/Put/Patch/Delete 等包级函数、客户端方法和辅助函数 |
| 配置 | Config 结构体、5 种预设配置、验证函数和 Cookie 安全 |
| 接口 | Client、Doer、DomainClienter、RetryPolicy 等核心接口 |
| Result | Result、RequestInfo、ResponseInfo、RequestMeta 类型和所有方法 |
| 处理器 | Handler 管线、MiddlewareFunc 洋葱链、Chain 组合器与 Mutator 契约 |
| 变更器 | RequestMutator/ResponseMutator 的读写方法与类型断言 |
请求与响应
| 模块 | 说明 |
|---|---|
| 请求选项 | 32 个 WithXxx 请求选项函数(请求头、请求体、认证、Cookie、回调、包级路由等) |
| 内置中间件 | Chain 组合、7 个内置中间件工厂和审计事件类型 |
| 错误类型 | ClientError、12 种 ErrorType 枚举和 17 个错误变量 |
高级功能
| 模块 | 说明 |
|---|---|
| 域名客户端 | DomainClient 创建、HTTP 方法、下载方法和 URL 拼接规则 |
| 会话管理 | SessionManager 的 Cookie/请求头管理和安全验证 |
| 文件下载 | 下载函数、DownloadConfig、断点续传和安全保护 |
| 常量与类型 | BodyKind 枚举、FormData/FileData 和审计上下文键 |
API 地图
按符号类型分组的索引,覆盖 github.com/cybergodev/httpc 包的主要导出面:逐项列出类型、函数与关键常量,成组的枚举常量(ErrorType*、BodyKind)与 Default*Config() 构造函数以组名汇总,逐项说明见链接的详情页。
客户端与包级函数
| 符号 | 说明 |
|---|---|
New / NewDefault | 创建客户端(自定义 / 默认配置) |
Get / Post / Put / Patch / Delete / Head / Options / Request | 包级 HTTP 方法(共享内部默认客户端) |
Download | 统一文件下载入口(包级函数 / Client / DomainClient 同名同签名) |
SetDefaultClient / CloseDefaultClient | 默认客户端替换与关闭 |
CloseRoutedClients | 关闭全部包级路由客户端(由 WithProxy/WithSystemProxy/WithTLSFingerprint 创建并缓存) |
NewDomain / NewDomainDefault | 创建域名客户端 |
SetSecurityWarnOutput | 安全警告输出重定向 |
FormatBytes / FormatSpeed | 字节数 / 速率格式化 |
请求选项(32 个)
| 分组 | 选项 |
|---|---|
| 请求头(3) | WithHeader、WithHeaderMap、WithUserAgent |
| 认证(2) | WithBasicAuth、WithBearerToken |
| 请求体(7) | WithJSON、WithXML、WithForm、WithFormData、WithFile、WithBinary、WithBody |
| 查询参数(2) | WithQuery、WithQueryMap |
| Cookie(4) | WithCookies、WithCookieMap、WithCookieString、WithSecureCookie |
| 请求控制(9) | WithContext、WithTimeout、WithNoTimeout、WithMaxRetries、WithRetryNonIdempotent、WithFollowRedirects、WithMaxRedirects、WithAllowPrivateIPs、WithStreamBody |
| 包级路由(3) | WithProxy、WithSystemProxy、WithTLSFingerprint |
| 回调(2) | WithOnRequest、WithOnResponse |
全部选项的签名、校验规则,以及各选项覆盖的 Config 默认值,见请求选项。
Result 家族
| 分类 | 符号 |
|---|---|
| 类型 | Result(17 个 nil 安全方法) |
| 状态与协议 | StatusCode、Proto、IsSuccess、IsRedirect、IsClientError、IsServerError |
| 请求体访问 | Body、RawBody |
| 解析与保存 | Unmarshal、SaveToFile、String |
| Cookie | ResponseCookies、GetCookie、HasCookie、RequestCookies、GetRequestCookie、HasRequestCookie |
| 子类型 | RequestInfo、ResponseInfo、RequestMeta(含 ProxyURL 代理字段) |
详见 Result。
配置
| 分类 | 符号 |
|---|---|
| 主类型 | Config(含 Timeouts / Connection / Security / Retry / Middleware / Defaults 六组) |
| 子配置类型 | TimeoutConfig、ConnectionConfig、SecurityConfig、RetryConfig、MiddlewareConfig、RequestDefaults |
| 预设(5 个) | DefaultConfig、SecureConfig、PerformanceConfig、TestingConfig、MinimalConfig |
| 校验与输出 | ValidateConfig、Config.String |
| Cookie 安全 | CookieSecurityConfig、DefaultCookieSecurityConfig、StrictCookieSecurityConfig |
| 下载配置 | DownloadConfig、DefaultDownloadConfig、DownloadResult、DownloadProgressCallback、ChecksumAlgorithm |
| 会话配置 | SessionConfig、DefaultSessionConfig、NewSessionManager、NewSessionManagerDefault |
Handler、中间件与变更器
| 分类 | 符号 |
|---|---|
| 管线类型 | Handler、MiddlewareFunc、Chain |
| 中间件工厂(7 个) | LoggingMiddleware、RecoveryMiddleware、RequestIDMiddleware、TimeoutMiddleware、HeaderMiddleware、MetricsMiddleware、AuditMiddleware |
| 中间件配置 | LoggingConfig、RequestIDConfig、TimeoutMiddlewareConfig、HeaderConfig、MetricsConfig、AuditConfig,以及各自的 DefaultLoggingConfig、DefaultRequestIDConfig、DefaultTimeoutMiddlewareConfig、DefaultHeaderConfig、DefaultMetricsConfig、DefaultAuditConfig |
| 变更器 | RequestMutator、ResponseMutator(中间件读写接口),以及只读/只写分体别名 RequestReader、RequestWriter、ResponseReader、ResponseWriter |
接口与类型
| 分类 | 符号 |
|---|---|
| 核心接口 | Client、Doer、DomainClienter、RetryPolicy |
| 选项类型 | RequestOption(接口)、RequestOptionFunc(函数适配器) |
| 类型别名 | ClientError、ErrorType、CertificatePinner、ProxyStrategy |
| 证书固定 | NewSPKIHashPinner、NewPublicKeyPinner、NewCertificatePinnerChain |
| 会话与域名 | SessionManager、DomainClient(建议通过 DomainClienter 接口使用) |
| 数据类型 | FormData、FileData、AuditEvent |
错误与常量
| 分类 | 符号 |
|---|---|
| 错误类型 | ClientError、ErrorType(12 种错误类别枚举:ErrorTypeUnknown、ErrorTypeNetwork、ErrorTypeTimeout、ErrorTypeContextCanceled、ErrorTypeResponseRead、ErrorTypeTransport、ErrorTypeRetryExhausted、ErrorTypeTLS、ErrorTypeCertificate、ErrorTypeDNS、ErrorTypeValidation、ErrorTypeHTTP) |
| 哨兵错误(17 个) | ErrClientClosed、ErrNilConfig、ErrInvalidHeader、ErrInvalidTimeout、ErrInvalidRetry、ErrInvalidMaxRedirects、ErrInvalidCredentials、ErrInvalidConnection、ErrInvalidSecurity、ErrInvalidMiddleware、ErrEmptyFilePath、ErrFileExists、ErrResponseBodyEmpty、ErrResponseBodyTooLarge、ErrStreamBodyRequiresDownload、ErrDownloadInProgress、ErrProxyConnectionFailed |
| BodyKind(6 常量) | BodyAuto、BodyJSON、BodyXML、BodyForm、BodyBinary、BodyMultipart |
| 其他常量 | ProxyStrategyRoundRobin / ProxyStrategyRandom、ChecksumSHA256、审计上下文键 SourceIPKey / UserIDKey |
快速参考
创建客户端
go
client, err := httpc.NewDefault() // 默认配置
client, err := httpc.New(httpc.SecureConfig()) // 安全预设
client, err := httpc.New(customConfig) // 自定义配置发送请求
go
// 包级函数
result, err := httpc.Get(url, options...)
// 客户端方法
result, err := client.Get(url, options...)
// 带上下文
result, err := client.Request(ctx, "GET", url, options...)处理响应
go
result.StatusCode() // 状态码
result.Body() // 响应体(字符串)
result.RawBody() // 响应体(字节)
result.Unmarshal(&data) // JSON 解析
result.IsSuccess() // 是否 2xx
result.Meta.Duration // 请求耗时
result.Meta.Attempts // 总尝试次数(含首次)版本兼容性
- Go 版本:要求 Go 1.27 及以上(
go.mod声明go 1.27)。 - 导入路径:
github.com/cybergodev/httpc(包名httpc,无需别名)。 - 直接依赖:7 个直接模块,均无条件参与编译(不存在按功能裁剪的构建标签;平台差异经
GOOS选择文件,如internal/engine/errors_unix.go)。golang.org/x/net与golang.org/x/sys服务标准路径;brotli / compress / uTLS / tls-client / fhttp 服务于传输层指纹路径——导入本包即会把它们编译进依赖图,未启用Security.TLSFingerprint时只是不参与运行。 - API 状态:当前全部导出符号均无
Deprecated标记,处于活跃维护状态。