{"id":16841115,"url":"https://github.com/bobheadxi/res","last_synced_at":"2025-03-18T05:15:01.568Z","repository":{"id":57492041,"uuid":"170082589","full_name":"bobheadxi/res","owner":"bobheadxi","description":"📫 Ergonomic primitives for working with JSON in RESTful Go servers and clients","archived":false,"fork":false,"pushed_at":"2020-02-05T19:59:35.000Z","size":15,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-19T22:48:24.397Z","etag":null,"topics":["api","golang","http","json","library","requests","responses","rest-api"],"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/bobheadxi.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":"2019-02-11T07:00:13.000Z","updated_at":"2020-11-14T07:09:04.000Z","dependencies_parsed_at":"2022-08-28T13:10:56.273Z","dependency_job_id":null,"html_url":"https://github.com/bobheadxi/res","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/bobheadxi%2Fres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobheadxi%2Fres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobheadxi%2Fres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobheadxi%2Fres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bobheadxi","download_url":"https://codeload.github.com/bobheadxi/res/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244160048,"owners_count":20408021,"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","golang","http","json","library","requests","responses","rest-api"],"created_at":"2024-10-13T12:40:21.232Z","updated_at":"2025-03-18T05:15:01.545Z","avatar_url":"https://github.com/bobheadxi.png","language":"Go","readme":"# res\n\nPackage res provides handy primitives for working with JSON in Go HTTP servers\nand clients via [`go-chi/render`](https://github.com/go-chi/render). It is\ndesigned to be lightweight and easy to extend.\n\n[![GoDoc](https://img.shields.io/badge/go.pkg.dev-reference-5272B4)](https://pkg.go.dev/go.bobheadxi.dev/res)\n[![CI Status](https://dev.azure.com/bobheadxi/bobheadxi/_apis/build/status/bobheadxi.res?branchName=master)](https://dev.azure.com/bobheadxi/bobheadxi/_build/latest?definitionId=1\u0026branchName=master)\n[![Go Report Card](https://goreportcard.com/badge/go.bobheadxi.dev/res)](https://goreportcard.com/report/go.bobheadxi.dev/res)\n![Sourcegraph for Repo Reference Count](https://img.shields.io/sourcegraph/rrc/github.com/bobheadxi/res.svg)\n\nI originally wrote something similar to this in two\n[UBC Launch Pad](https://www.ubclaunchpad.com/) projects that I worked on -\n[Inertia](https://github.com/ubclaunchpad/inertia) and\n[Pinpoint](https://github.com/ubclaunchpad/pinpoint) - and felt like it might\nbe useful to have it as a standalone package.\n\nIt is currently a work-in-progress - I'm hoping to continue refining the API\nand add more robust tests.\n\n## Usage\n\n```sh\ngo get -u go.bobheadxi.dev/res\n```\n\n### Clientside\n\nI implemented something similar to `res` in [Inertia](https://github.com/ubclaunchpad/inertia).\nIt has a client that shows how you might leverage this library:\n[`inertia/client.Client`](https://github.com/ubclaunchpad/inertia/blob/master/client/client.go)\n\n```go\nimport \"go.bobheadxi.dev/res\"\n\nfunc main() {\n  resp, err := http.Get(os.Getenv(\"URL\"))\n  if err != nil {\n    log.Fatal(err)\n  }\n  var info string\n  b, err := res.Unmarshal(resp.Body, res.KV{Key: \"info\", Value: \u0026info})\n  if err != nil {\n    log.Fatal(err)\n  }\n  if err := b.Error(); err != nil {\n    log.Fatal(err)\n  }\n  println(info)\n}\n```\n\n### Serverside\n\n#### OK\n\n```go\nimport \"go.bobheadxi.dev/res\"\n\nfunc Handler(w http.ResponseWriter, r *http.Request) {\n  res.R(w, r, res.MsgOK(\"hello world!\",\n    \"stuff\", \"amazing\",\n    \"details\", res.M{\"world\": \"hello\"}))\n}\n```\n\nWill render something like:\n\n```js\n{\n  \"code\": 200,\n  \"message\": \"hello world\",\n  \"request_id\": \"12345\",\n  \"body\": {\n    \"stuff\": \"amazing\",\n    \"details\": {\n      \"world\": \"hello\",\n    }\n  }\n}\n```\n\n#### Error\n\n```go\nimport \"go.bobheadxi.dev/res\"\n\nfunc Handler(w http.ResponseWriter, r *http.Request) {\n  body, err := ioutil.ReadAll(r.Body)\n  if err != nil {\n    res.R(w, r, res.ErrBadRequest(\"failed to read request\",\n      \"error\", err,\n      \"details\", \"something\"))\n    return\n  }\n}\n```\n\nWill render something like:\n\n```js\n{\n  \"code\": 400,\n  \"message\": \"failed to read request\",\n  \"request_id\": \"12345\",\n  \"error\": \"could not read body\",\n  \"body\": {\n    \"details\": \"something\",\n  }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbobheadxi%2Fres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbobheadxi%2Fres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbobheadxi%2Fres/lists"}