{"id":37133036,"url":"https://github.com/xfali/restclient","last_synced_at":"2026-01-14T15:32:09.676Z","repository":{"id":36592918,"uuid":"228632572","full_name":"xfali/restclient","owner":"xfali","description":"restclient是一个简单易用，灵活可扩展的RESTFUL客户端（连接池）， 支持Basic Auth、Digest Auth、Token Auth，内置多个interface转换器","archived":false,"fork":false,"pushed_at":"2024-07-11T12:02:59.000Z","size":155,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"v2","last_synced_at":"2025-08-09T21:37:46.348Z","etag":null,"topics":["auth","basic","digest","golang","http-client","rest-client","token"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xfali.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}},"created_at":"2019-12-17T14:19:41.000Z","updated_at":"2024-12-02T07:09:31.000Z","dependencies_parsed_at":"2024-07-11T13:14:31.235Z","dependency_job_id":"8254da1c-550b-4d5a-b27e-76b702ae0cf0","html_url":"https://github.com/xfali/restclient","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/xfali/restclient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfali%2Frestclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfali%2Frestclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfali%2Frestclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfali%2Frestclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xfali","download_url":"https://codeload.github.com/xfali/restclient/tar.gz/refs/heads/v2","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xfali%2Frestclient/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28424374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["auth","basic","digest","golang","http-client","rest-client","token"],"created_at":"2026-01-14T15:32:08.893Z","updated_at":"2026-01-14T15:32:09.669Z","avatar_url":"https://github.com/xfali.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# restclient\n\n## 介绍 \n\n  restclient是一个简单易用的RESTFUL客户端（连接池）。\n  \n  内置interface转换器：\n  - bytes\n  - string\n  - xml\n  - json\n  - yaml\n  \n  内置支持认证方式：\n  1. Basic Auth\n  2. Digest Auth\n  3. Token Auth\n  \n## 安装\n\n使用命令安装：\n\n```\ngo get github.com/xfali/restclient/v2\n```\n\n## 配置\n\n### 基础配置\n\n可以在创建默认client时对其进行配置，支持的options请参照[options](init_opts.go)的API说明\n\n使用为\n```\n// restclient.New(Option1, Option2 ... OptionN), 例:\nclient := restclient.New(restclient.SetTimeout(10*time.Second))\n```\n```\n//设置读写超时\nrestclient.SetTimeout(timeout time.Duration)\n```\n```\n//配置初始转换器列表\nrestclient.SetConverters(convs []Converter)\n```\n```\n//配置连接池\nrestclient.SetRoundTripper(tripper http.RoundTripper)\n```\n```\n// 增加处理filter\nrestclient.AddFilter(filters ...Filter)\n```\n```\n//配置request创建器\nrestclient.SetRequestCreator(f RequestCreator)\n```\n```\n// CookieJar 配置http.Client的CookieJar\nrestclient.CookieJar(jar http.CookieJar)\n```\n```\n// 配置http客户端创建器\nrestclient.SetClientCreator(cliCreator HttpClientCreator)\n```\n### 连接池配置\n\n请参照http.transport的API说明\n\n## 使用\n1. 使用request传递http请求参数\n```\n//使用默认配置\nclient := restclient.New()\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n```\n2. 使用request builder创建和传递http请求参数\n```\nerr := client.Exchange(\"http://localhost:8080/error\",\n    restclient.NewRequest().\n        MethodPost().\n        RequestBody(req).\n        Result(\u0026resp).\n        Build())\n```\n\n## 扩展\n\n使用filter.Filter进行行为控制和扩展功能，如增加client的输入输出日志：\n```\nclient := restclient.New(restclient.AddIFilter(filter.NewLog(xlog.GetLogger(), \"\")))\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n```\n可以自行实现IFilter接口，并注册到restclient扩展其功能\n\n## 认证\n\n### Basic Auth\n\n```\no := restclient.New(restclient.SetTimeout(time.Second))\nauth := filter.NewBasicAuth(\"user\", \"password\")\nclient := restclient.New(restclient.AddIFilter(auth))\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n\n//change username and password\nauth.ResetCredentials(username, password)\n```\n\n### Digest Auth\n\n```\nauth := filter.NewDigestAuth(\"user\", \"password\")\nclient := restclient.New(restclient.AddIFilter(auth))\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n\n//change username and password\nauth.ResetCredentials(username, password)\n```\n\n### Token Auth\n\n```\nauth := filter.NewAccessTokenAuth(\"{TOKEN}\")\nclient := restclient.New(restclient.AddIFilter(auth))\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n\n//change username and password\nauth.ResetCredentials(\"{TOKEN}\")\n```\n\n### 带日志client\n```\nclient := restclient.New(restclient.AddIFilter(filter.NewLog(xlog.GetLogger(), \"\")))\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n```\n\n### 捕捉panic\n```\nclient := restclient.New(restclient.AddIFilter(filter.NewRecovery(xlog.GetLogger())))\nresp := \u0026Response{}\nerr := client.Exchange(\"http://localhost:8080/test\",\n    request.WithResult(\u0026ret),\n    request.WithResponse(resp, false))\n```\n\n## UrlBuilder\n可以使用restclient.NewUrlBuilder为url添加参数，快速构建请求路径\n```\nbuilder := restclient.NewUrlBuilder(\"x/:a/tt/:b?\")\nbuilder.PathVariable(\"a\", \"1\")\nbuilder.PathVariable(\"b\", 2)\nbuilder.QueryVariable(\"c\", 100)\nbuilder.QueryVariable(\"d\", 1.1)\nurl := builder.Build()\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxfali%2Frestclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxfali%2Frestclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxfali%2Frestclient/lists"}