{"id":37821283,"url":"https://github.com/wjqserver-studio/httpc","last_synced_at":"2026-04-23T12:01:40.413Z","repository":{"id":291500444,"uuid":"976918198","full_name":"WJQSERVER-STUDIO/httpc","owner":"WJQSERVER-STUDIO","description":"Touka HTTPC 现代化的Go HTTPC实现","archived":false,"fork":false,"pushed_at":"2026-03-22T03:34:29.000Z","size":134,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-02T08:39:28.979Z","etag":null,"topics":["go","golang","http","http-client"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WJQSERVER-STUDIO.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-05-03T02:30:02.000Z","updated_at":"2026-03-22T03:34:32.000Z","dependencies_parsed_at":"2025-05-22T03:30:34.579Z","dependency_job_id":"1bc89e12-aa68-4810-a926-ba0c8a0e73aa","html_url":"https://github.com/WJQSERVER-STUDIO/httpc","commit_stats":null,"previous_names":["wjqserver-studio/httpc"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/WJQSERVER-STUDIO/httpc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WJQSERVER-STUDIO%2Fhttpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WJQSERVER-STUDIO%2Fhttpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WJQSERVER-STUDIO%2Fhttpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WJQSERVER-STUDIO%2Fhttpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WJQSERVER-STUDIO","download_url":"https://codeload.github.com/WJQSERVER-STUDIO/httpc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WJQSERVER-STUDIO%2Fhttpc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32179387,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-23T11:42:27.955Z","status":"ssl_error","status_checked_at":"2026-04-23T11:42:18.877Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","http","http-client"],"created_at":"2026-01-16T15:42:09.909Z","updated_at":"2026-04-23T12:01:40.407Z","avatar_url":"https://github.com/WJQSERVER-STUDIO.png","language":"Go","readme":"# httpc\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/WJQSERVER-STUDIO/httpc)](https://goreportcard.com/report/github.com/WJQSERVER-STUDIO/httpc)\n[![GoDoc](https://godoc.org/github.com/WJQSERVER-STUDIO/httpc?status.svg)](https://godoc.org/github.com/WJQSERVER-STUDIO/httpc)\n[![License](https://img.shields.io/badge/license-MPL--2.0%20%2F%20WJQserver--2.0-blue.svg)](LICENSE)\n\n`httpc` 是一个基于 Go 标准库 `net/http` 构建的高级 HTTP 客户端库。它提供了链式调用、自动重试、中间件支持、响应自动解码以及详细的日志记录等功能，旨在简化日常开发中的 HTTP 请求处理。\n\n## 特性\n\n- **流式 API**：支持链式调用，轻松构建复杂的请求。\n- **自动重试**：内置指数退避 (Exponential Backoff) 和抖动 (Jitter) 策略。\n- **响应解码**：支持 JSON、XML、GOB、字符串及字节流的自动解码。\n- **中间件支持**：允许在请求生命周期内注入自定义逻辑。\n- **性能优化**：通过 `sync.Pool` 复用字节缓冲区，降低内存分配开销。\n- **深度可调**：支持对底层 `Transport` 和 `Dialer` 的精细配置。\n- **HTTP/2 \u0026 H2C**：完整支持 HTTP/1.1、HTTP/2 以及非加密的 H2C 协议。\n\n## 安装\n\n```bash\ngo get github.com/WJQSERVER-STUDIO/httpc\n```\n\n## 快速开始\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/WJQSERVER-STUDIO/httpc\"\n)\n\ntype User struct {\n\tID   int    `json:\"id\"`\n\tName string `json:\"name\"`\n}\n\nfunc main() {\n\t// 创建并配置客户端\n\tclient := httpc.New(\n\t\thttpc.WithTimeout(15 * time.Second),\n\t\thttpc.WithDumpLog(), // 启用请求日志\n\t)\n\n\tctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\n\tdefer cancel()\n\n\tvar user User\n\t// 链式调用发送 GET 请求并解码 JSON\n\terr := client.GET(\"https://api.example.com/users/1\").\n\t\tWithContext(ctx).\n\t\tSetHeader(\"Accept\", \"application/json\").\n\t\tAddQueryParam(\"details\", \"true\").\n\t\tDecodeJSON(\u0026user)\n\n\tif err != nil {\n\t\tif httpErr, ok := err.(*httpc.HTTPError); ok {\n\t\t\tlog.Printf(\"HTTP Error: %d, Body: %s\", httpErr.StatusCode, string(httpErr.Body))\n\t\t} else {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\treturn\n\t}\n\n\tfmt.Printf(\"User: %+v\\n\", user)\n}\n```\n\n## 文档\n\n更多详细指南请参阅以下文档：\n\n- [客户端配置 (Client)](docs/client.md)\n- [请求构建 (RequestBuilder)](docs/builder.md)\n- [响应处理 (Response)](docs/response.md)\n- [重试与中间件 (Retry \u0026 Middleware)](docs/retry-middleware.md)\n- [底层传输与协议 (Transport \u0026 Protocols)](docs/transport.md)\n- [API 参考 (API Index)](docs/api.md)\n\n## 核心功能\n\n### 1. 请求构建器 (RequestBuilder)\n\n`RequestBuilder` 提供了丰富的接口来设置请求参数：\n\n```go\nrb := client.POST(\"https://api.example.com/data\").\n    SetHeaders(map[string]string{\"Authorization\": \"Bearer token\"}).\n    SetQueryParams(map[string]string{\"page\": \"1\", \"limit\": \"20\"}).\n    SetJSONBody(map[string]interface{}{\"foo\": \"bar\"})\n\n// 执行请求并获取原始响应\nresp, err := rb.Execute()\nif err == nil {\n    defer resp.Body.Close()\n}\n```\n\n### 2. 自动重试策略\n\n默认启用 2 次重试（共执行 3 次），支持针对特定状态码进行指数退避。\n\n```go\nclient := httpc.New(\n    httpc.WithRetryOptions(httpc.RetryOptions{\n        MaxAttempts:   3,\n        BaseDelay:     200 * time.Millisecond,\n        MaxDelay:      2 * time.Second,\n        RetryStatuses: []int{429, 500, 503},\n        Jitter:        true,\n    }),\n)\n```\n\n### 3. 中间件\n\n中间件签名遵循 `func(next http.Handler) http.Handler`，方便与标准库生态集成。\n\n```go\nfunc LoggingMiddleware(next http.Handler) http.Handler {\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        start := time.Now()\n        next.ServeHTTP(w, r)\n        log.Printf(\"%s %s took %v\", r.Method, r.URL.Path, time.Since(start))\n    })\n}\n\nclient := httpc.New(httpc.WithMiddleware(LoggingMiddleware))\n```\n\n### 4. 详细日志记录\n\n通过 `WithDumpLog` 可以输出结构化的请求调试日志，包括请求头、协议版本及底层 Transport 配置。\n\n## 详细配置\n\n| 选项 | 说明 |\n| :--- | :--- |\n| `WithTimeout` | 设置全局请求超时 |\n| `WithUserAgent` | 自定义 User-Agent |\n| `WithTransport` | 替换或扩展底层 `http.Transport` |\n| `WithProtocols` | 配置支持的 HTTP 协议 (HTTP1/2/H2C) |\n| `WithMiddleware` | 注册全局中间件 |\n\n## 错误处理\n\n`httpc` 会在状态码 \u003e= 400 时返回 `*httpc.HTTPError`。你可以通过它获取响应头、状态码以及响应体的前几位预览，方便调试。\n\n## 许可证\n\n本项目采用 **MPL-2.0** 与 **WJQserver Studio License 2.0** 双重授权。您可以任选其一使用。所有权利由 satomitouka 保留，WJQserver Studio 代行相关权利。\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwjqserver-studio%2Fhttpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwjqserver-studio%2Fhttpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwjqserver-studio%2Fhttpc/lists"}