{"id":23682928,"url":"https://github.com/johejo/go-content-encoding","last_synced_at":"2025-09-02T11:31:24.186Z","repository":{"id":38419711,"uuid":"271808580","full_name":"johejo/go-content-encoding","owner":"johejo","description":"HTTP Content-Encoding decoder for Go","archived":false,"fork":false,"pushed_at":"2022-08-02T20:36:55.000Z","size":32,"stargazers_count":1,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-19T11:34:13.155Z","etag":null,"topics":["content-encoding","go","golang","http","middleware"],"latest_commit_sha":null,"homepage":null,"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/johejo.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}},"created_at":"2020-06-12T13:46:26.000Z","updated_at":"2024-06-19T11:34:13.156Z","dependencies_parsed_at":"2022-07-14T03:30:31.680Z","dependency_job_id":null,"html_url":"https://github.com/johejo/go-content-encoding","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johejo%2Fgo-content-encoding","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johejo%2Fgo-content-encoding/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johejo%2Fgo-content-encoding/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johejo%2Fgo-content-encoding/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johejo","download_url":"https://codeload.github.com/johejo/go-content-encoding/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231780340,"owners_count":18425542,"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":["content-encoding","go","golang","http","middleware"],"created_at":"2024-12-29T19:59:30.982Z","updated_at":"2024-12-29T19:59:31.626Z","avatar_url":"https://github.com/johejo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-content-encoding\n\n[![ci](https://github.com/johejo/go-content-encoding/workflows/ci/badge.svg?branch=master)](https://github.com/johejo/go-content-encoding/actions?query=workflow%3Aci)\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/github.com/johejo/go-content-encoding)\n[![codecov](https://codecov.io/gh/johejo/go-content-encoding/branch/master/graph/badge.svg)](https://codecov.io/gh/johejo/go-content-encoding)\n[![Go Report Card](https://goreportcard.com/badge/github.com/johejo/go-content-encoding)](https://goreportcard.com/report/github.com/johejo/go-content-encoding)\n\n## Description\n\ngo-content-encoding provides net/http compatible middleware for HTTP Content-Encoding.\u003cbr\u003e\nIt also provides the functionality to customize the decoder.\u003cbr\u003e\nBy default, br(brotli), gzip and zstd(zstandard) are supported.\n\n## Example\n\n```go\npackage contentencoding_test\n\nimport (\n\t\"fmt\"\n\t\"io/ioutil\"\n\t\"log\"\n\t\"net/http\"\n\t\"net/http/httptest\"\n\t\"strings\"\n\n\tcontentencoding \"github.com/johejo/go-content-encoding\"\n)\n\nfunc ExampleDecode() {\n\thandler := func(w http.ResponseWriter, r *http.Request) {\n\t\tb, err := ioutil.ReadAll(r.Body)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tlog.Println(b) // decoded body\n\t}\n\n\tmux := http.NewServeMux()\n\tdecode := contentencoding.Decode()\n\tmux.Handle(\"/\", decode(http.HandlerFunc(handler)))\n}\n\nfunc ExampleWithDecoder() {\n\tcustomDecoder := \u0026contentencoding.Decoder{\n\t\tEncoding: \"custom\",\n\t\tHandler: func(w http.ResponseWriter, r *http.Request) error {\n\t\t\tb, err := ioutil.ReadAll(r.Body)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tr.Body = ioutil.NopCloser(strings.NewReader(string(b) + \"-custom\"))\n\t\t\treturn nil\n\t\t},\n\t}\n\tmux := http.NewServeMux()\n\tdm := contentencoding.Decode(contentencoding.WithDecoder(customDecoder))\n\tmux.Handle(\"/\", dm(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tb, err := ioutil.ReadAll(r.Body)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tfmt.Println(string(b))\n\t})))\n\trec := httptest.NewRecorder()\n\treq := httptest.NewRequest(http.MethodPost, \"/\", strings.NewReader(\"test\"))\n\treq.Header.Set(\"Content-Encoding\", \"custom\")\n\tmux.ServeHTTP(rec, req)\n\n\t// Output:\n\t// test-custom\n}\n\nfunc ExampleWithErrorHandler() {\n\tmux := http.NewServeMux()\n\terrHandler := contentencoding.ErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {\n\t\tw.WriteHeader(999) // custom error code\n\t})\n\tdm := contentencoding.Decode(contentencoding.WithErrorHandler(errHandler))\n\tmux.Handle(\"/\", dm(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})))\n\trec := httptest.NewRecorder()\n\treq := httptest.NewRequest(http.MethodPost, \"/\", strings.NewReader(\"test\")) // not compressed\n\treq.Header.Set(\"Content-Encoding\", \"gzip\")\n\tmux.ServeHTTP(rec, req)\n\tfmt.Println(rec.Code)\n\n\t// Output:\n\t// 999\n}\n```\n\n\n## License\n\nMIT\n\n## Author\n\nMitsuo Heijo (@johejo)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohejo%2Fgo-content-encoding","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohejo%2Fgo-content-encoding","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohejo%2Fgo-content-encoding/lists"}