{"id":15514454,"url":"https://github.com/petems/client-inspect","last_synced_at":"2025-03-28T19:21:05.118Z","repository":{"id":57547896,"uuid":"303531656","full_name":"petems/client-inspect","owner":"petems","description":"A library that injects an inspector to a Golang http.Client object to show traffic","archived":false,"fork":false,"pushed_at":"2020-11-01T09:56:35.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-02T20:49:21.359Z","etag":null,"topics":["golang","http-client","inspection"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petems.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-12T22:54:13.000Z","updated_at":"2021-07-24T18:01:26.000Z","dependencies_parsed_at":"2022-09-26T18:41:04.044Z","dependency_job_id":null,"html_url":"https://github.com/petems/client-inspect","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petems%2Fclient-inspect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petems%2Fclient-inspect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petems%2Fclient-inspect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petems%2Fclient-inspect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petems","download_url":"https://codeload.github.com/petems/client-inspect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246085655,"owners_count":20721213,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["golang","http-client","inspection"],"created_at":"2024-10-02T09:59:16.843Z","updated_at":"2025-03-28T19:21:05.094Z","avatar_url":"https://github.com/petems.png","language":"Go","readme":"# client-inspect\n[![GoDoc](https://godoc.org/github.com/petems/client-inspect/http?status.svg)](https://godoc.org/github.com/petems/client-inspect/http) [![PkgGoDev](https://pkg.go.dev/badge/petems/client-inspect/http)](https://pkg.go.dev/github.com/petems/client-inspect/http) [![Go Report Card](https://goreportcard.com/badge/github.com/petems/client-inspect)](https://goreportcard.com/report/github.com/petems/client-inspect)\n\nAllows inspection of a `http.Client` connections\n\nHeavily based on https://github.com/j0hnsmith/connspy\n\n### `http` package \n\nA `http.Client` suitable for debugging, writes all http data to stdout.\n\n```go\nimport (\n  \"bytes\"\n  \"fmt\"\n  \"io/ioutil\"\n  \"os\"\n  \"regexp\"\n  \"strings\"\n\n  clientInspect \"github.com/petems/client-inspect/http\"\n)\n\nfunc main() { \n\n  client := clientInspect.NewClient(nil, nil)\n\n  resp, _ := client.Get(\"http://example.com/\")\n  // ensure all of the body is read\n  ioutil.ReadAll(resp.Body)\n  resp.Body.Close()\n\n  resp, _ = client.Get(\"https://example.com/\")\n  ioutil.ReadAll(resp.Body)\n  resp.Body.Close()\n\n}\n```\n\n![image](https://user-images.githubusercontent.com/1064715/95797908-8d72f780-0ce8-11eb-97d7-5086f57c5e99.png)\n\nYou can also specify the writer with `http.NewClientWriter`, which can be used to do things like redact certain fields:\n\n```go\nimport (\n  \"bytes\"\n  \"fmt\"\n  \"io/ioutil\"\n  \"os\"\n  \"regexp\"\n  \"strings\"\n\n  clientInspect \"github.com/petems/client-inspect/http\"\n)\n\nfunc main() { \n\n  buf := new(bytes.Buffer)\n\n  client := http.NewClientWriter(nil, nil, buf)\n\n  resp, _ := client.Get(\"http://example.com/\")\n  // ensure all of the body is read\n  ioutil.ReadAll(resp.Body)\n  resp.Body.Close()\n\n  resp, _ = client.Get(\"https://example.com/\")\n  ioutil.ReadAll(resp.Body)\n  resp.Body.Close()\n\n  httpLog := buf.String()\n\n  s := strings.Split(httpLog, \"\\n\")\n\n  for count, line := range s {\n    rgx := regexp.MustCompile(`^(Host: )(.+)$`)\n    line = rgx.ReplaceAllString(line, `$1[REDACTED]`)\n    s[count] = line\n  }\n\n  fmt.Println(s)\n\n}\n```\n\n![image](https://user-images.githubusercontent.com/1064715/95797941-a5e31200-0ce8-11eb-95ab-c0adaa3f330d.png)\n\nFor more complex redaction purposes, I've been having a lot of luck with creating an io.Writer with logrus, then using [redactrus](https://github.com/whuang8/redactrus) to redact certain parts of the logs. Plus you can do cool formatting!\n\n```go\npackage main\n\nimport (\n\t\"io/ioutil\"\n\t\"time\"\n\n\t\"github.com/petems/client-inspect/http\"\n\t\"github.com/sirupsen/logrus\"\n\t\"github.com/whuang8/redactrus\"\n\tprefixed \"github.com/x-cray/logrus-prefixed-formatter\"\n)\n\nfunc main() {\n\n\trh := \u0026redactrus.Hook{\n\t\tAcceptedLevels: logrus.AllLevels,\n\t\tRedactionList:  []string{\"^(Host: ).+$\"},\n\t}\n\n\tlog := logrus.New()\n\n\ttextFormatter := new(prefixed.TextFormatter)\n\ttextFormatter.FullTimestamp = true\n\ttextFormatter.TimestampFormat = time.RFC822\n\n\tlog.SetFormatter(textFormatter)\n\n\tlog.AddHook(rh)\n\n\tclient := http.NewClientWriter(nil, nil, log.Writer())\n\n\tresp, _ := client.Get(\"http://example.com/\")\n\t// ensure all of the body is read\n\tioutil.ReadAll(resp.Body)\n\tresp.Body.Close()\n\n\tresp, _ = client.Get(\"https://example.com/\")\n\tioutil.ReadAll(resp.Body)\n\tresp.Body.Close()\n\n}\n```\n\n![image](https://user-images.githubusercontent.com/1064715/95857163-1c1c5e80-0d53-11eb-9748-a5232a0be94a.png)\n\n## Background info\n\n[https://medium.com/@j0hnsmith/eavesdrop-on-a-golang-http-client-c4dc49af9d5e](https://medium.com/@j0hnsmith/eavesdrop-on-a-golang-http-client-c4dc49af9d5e)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetems%2Fclient-inspect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetems%2Fclient-inspect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetems%2Fclient-inspect/lists"}