{"id":13413255,"url":"https://github.com/nicklaw5/go-respond","last_synced_at":"2025-04-29T23:49:17.289Z","repository":{"id":46801668,"uuid":"84757077","full_name":"nicklaw5/go-respond","owner":"nicklaw5","description":"A Go package for handling common HTTP JSON responses.","archived":false,"fork":false,"pushed_at":"2021-09-24T20:08:26.000Z","size":42,"stargazers_count":54,"open_issues_count":1,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-29T23:49:11.212Z","etag":null,"topics":["api","go","golang","json","json-api"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/nicklaw5/go-respond","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/nicklaw5.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":"2017-03-12T21:00:54.000Z","updated_at":"2025-02-11T21:33:12.000Z","dependencies_parsed_at":"2022-09-26T18:21:24.914Z","dependency_job_id":null,"html_url":"https://github.com/nicklaw5/go-respond","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklaw5%2Fgo-respond","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklaw5%2Fgo-respond/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklaw5%2Fgo-respond/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklaw5%2Fgo-respond/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicklaw5","download_url":"https://codeload.github.com/nicklaw5/go-respond/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251602602,"owners_count":21615957,"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":["api","go","golang","json","json-api"],"created_at":"2024-07-30T20:01:36.202Z","updated_at":"2025-04-29T23:49:17.268Z","avatar_url":"https://github.com/nicklaw5.png","language":"Go","funding_links":[],"categories":["Utilities","JSON","Relational Databases","实用工具","工具库"],"sub_categories":["SQL 查询语句构建库","Search and Analytic Databases","Advanced Console UIs","检索及分析资料库","交流","HTTP Clients","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# go-respond\n\nA Go package for handling common HTTP JSON responses.\n\n[![GoDoc](https://godoc.org/github.com/nicklaw5/go-respond?status.svg)](https://godoc.org/github.com/nicklaw5/go-respond)\n[![Build Status](https://travis-ci.org/nicklaw5/go-respond.svg?branch=master)](https://travis-ci.org/nicklaw5/go-respond)\n[![Coverage Status](https://coveralls.io/repos/github/nicklaw5/go-respond/badge.svg)](https://coveralls.io/github/nicklaw5/go-respond)\n[![Go Report Card](https://goreportcard.com/badge/github.com/nicklaw5/go-respond)](https://goreportcard.com/report/github.com/nicklaw5/go-respond)\n\n## Installation\n\n```bash\ngo get github.com/nicklaw5/go-respond\n```\n\n## Usage\n\nThe goal of `go-respond` is to take most of the grunt work out preparing your JSON response. Here's a simple example:\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n\n    resp \"github.com/nicklaw5/go-respond\"\n)\n\ntype User struct {\n    ID    int    `json:\"id\"`\n    Name  string `json:\"name\"`\n    Email string `json:\"email\"`\n}\n\nfunc main() {\n    http.HandleFunc(\"/api/users\", func(w http.ResponseWriter, r *http.Request) {\n        users := []User{\n            {1, \"Billy\", \"billy@example.com\"},\n            {2, \"Joan\", \"joan@example.com\"},\n        }\n\n        resp.NewResponse(w).Ok(users)\n    })\n\n    http.ListenAndServe(\":8080\", nil)\n}\n```\n\n## Response Methods\n\n| Response Code | Method Name |\n| :---------- | :------------ |\n| 200 | Ok() |\n| 201 | Created() |\n| 202 | Accepted() |\n| 204 | NoContent() |\n| 400 | BadRequest() |\n| 401 | Unauthorized() |\n| 403 | Forbidden() |\n| 404 | NotFound() |\n| 405 | MethodNotAllowed() |\n| 406 | NotAcceptable() |\n| 409 | Conflict() |\n| 410 | Gone() |\n| 411 | LengthRequired() |\n| 412 | PreconditionFailed() |\n| 413 | RequestEntityTooLarge() |\n| 415 | UnsupportedMediaType() |\n| 422 | UnprocessableEntity() |\n| 500 | InternalServerError() |\n| 501 | NotImplemented() |\n| 502 | BadGateway() |\n| 503 | ServiceUnavailable() |\n| 504 | GatewayTimeout() |\n\nSee [here](https://httpstatuses.com/) for a complete list of HTTP responses, along with an explanation of each.\n\nPlease submit a PR if you want to add to this list. Only the most common response types have been included.\n\n## To Long, Don't Write\n\nSometimes you don't need to return a specific content-message but don't want the response body to be empty.\nIn this case you can use the `DefaultMessage()` for responding with json containing the default message for the corresponding status code.\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n    resp \"github.com/nicklaw5/go-respond\"\n)\n\nfunc main() {\n    http.HandleFunc(\"/api/users\", func(w http.ResponseWriter, r *http.Request) {\n        // ...\n        if !authenticated {\n            resp.NewResponse(w).DefaultMessage().\n                Unauthorized(nil)\n        }\n        // ...\n    })\n    http.ListenAndServe(\":8080\", nil)\n}\n```\n\nWould respond with `{\"status\":401,\"message\":\"Unauthorized\"}`\n\n## Handling Errors\n\nThe best option for handling errors that may occur while marshalling the JSON response, is to use [Negroni's Recovery middleware](https://github.com/urfave/negroni#recovery). Here's an example:\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n\n    \"github.com/urfave/negroni\"\n    resp \"github.com/nicklaw5/go-respond\"\n)\n\ntype Response struct {\n    Success bool `json:\"success\"`\n}\n\nfunc main() {\n    mux := http.NewServeMux()\n    mux.HandleFunc(\"/\", func(w http.ResponseWriter, req *http.Request) {\n        resp.NewResponse(w).Ok(\u0026Response{true})\n    })\n\n    n := negroni.New()\n    recovery := negroni.NewRecovery()\n    recovery.ErrorHandlerFunc = func(error interface{}) {\n        // do something with the unexpected error\n    }\n\n    n.Use(recovery)\n    n.UseHandler(mux)\n\n    http.ListenAndServe(\":8080\", n)\n}\n```\n\n## License\n\nThis package is distributed under the terms of the [MIT](LICENSE) License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklaw5%2Fgo-respond","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicklaw5%2Fgo-respond","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklaw5%2Fgo-respond/lists"}