{"id":16828024,"url":"https://github.com/kataras/requestid","last_synced_at":"2025-04-11T03:51:15.546Z","repository":{"id":57616791,"uuid":"279084235","full_name":"kataras/requestid","owner":"kataras","description":"Unique Identifier for each HTTP request","archived":false,"fork":false,"pushed_at":"2023-10-26T21:55:54.000Z","size":16,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T01:51:13.287Z","etag":null,"topics":["go","golang","http","iris","requestid","unique-identifier"],"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/kataras.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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},"funding":{"github":"kataras"}},"created_at":"2020-07-12T14:43:15.000Z","updated_at":"2023-08-19T03:34:40.000Z","dependencies_parsed_at":"2024-06-20T22:12:03.515Z","dependency_job_id":null,"html_url":"https://github.com/kataras/requestid","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frequestid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frequestid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frequestid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Frequestid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kataras","download_url":"https://codeload.github.com/kataras/requestid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248339262,"owners_count":21087214,"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":["go","golang","http","iris","requestid","unique-identifier"],"created_at":"2024-10-13T11:24:12.008Z","updated_at":"2025-04-11T03:51:15.530Z","avatar_url":"https://github.com/kataras.png","language":"Go","funding_links":["https://github.com/sponsors/kataras"],"categories":[],"sub_categories":[],"readme":"# Request ID\r\n\r\n[![build status](https://img.shields.io/github/actions/workflow/status/kataras/requestid/ci.yml?style=for-the-badge)](https://github.com/kataras/requestid/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/requestid) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://godoc.org/github.com/kataras/requestid)\r\n\r\nUnique Identifier for each HTTP request. Useful for logging, propagation and e.t.c.\r\n\r\n## Installation\r\n\r\nThe only requirement is the [Go Programming Language](https://golang.org/dl).\r\n\r\n```sh\r\n$ go get github.com/kataras/requestid\r\n```\r\n\r\n## Getting Started\r\n\r\nImport the package:\r\n\r\n```go\r\npackage main\r\n\r\nimport \"github.com/kataras/requestid\"\r\n```\r\n\r\nWrap a handler with the `Handler` function and retrieve the request ID using the `Get` function:\r\n\r\n```go\r\nimport \"net/http\"\r\n\r\nfunc main() {\r\n    mux := http.NewServeMux()\r\n    mux.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request){\r\n        id:= requestid.Get(r)\r\n        w.Write([]byte(id))\r\n    })\r\n\r\n    http.ListenAndServe(\":8080\", requestid.Handler(mux))\r\n}\r\n```\r\n\r\nBy-default the `requestid` middleware uses the `X-Request-Id` header to extract and set the request ID.\r\nIt generates a [universally unique identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)) when the request header is missing. Use custom logic to extract and set the request ID using `HandlerWithGenerator`:\r\n\r\n```go\r\nimport \"net/http\"\r\n\r\nfunc main() {\r\n    // extract from a request header and set to the response header.\r\n    gen := func(w http.ResponseWriter, r *http.Request) string {\r\n        id:= r.Header.Get(\"X-Custom-Id\")\r\n        if id == \"\" {\r\n            // [custom logic to generate ID...]\r\n        }\r\n        w.Header().Set(\"X-Custom-Id\", id)\r\n        return id\r\n    }\r\n\r\n    // [...]\r\n    router := requestid.HandlerWithGenerator(mux, gen)\r\n    http.ListenAndServe(\":8080\", router)\r\n}\r\n```\r\n\r\nWhen you want an identifier of request based on the headers, body and e.t.c. use the `HashGenerator` helper. Note that, the request id will be the same if the same client sends the same requests (that's the goal here):\r\n\r\n```go\r\nfunc main() {\r\n    // [...]\r\n\r\n    includeBodyOnHash := false\r\n    gen := requestid.HashGenerator(includeBodyOnHash)\r\n\r\n    requestid.HandlerWithGenerator(mux, gen)\r\n\r\n    // [...]\r\n}\r\n```\r\n \r\n## License\r\n\r\nThis software is licensed under the [MIT License](LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkataras%2Frequestid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkataras%2Frequestid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkataras%2Frequestid/lists"}