RESTful Primitives Library
Package res
provides handy primitives for working with JSON in Go HTTP servers and clients via go-chi/render
. It is designed to be lightweight and easy to extend.
I originally wrote something similar to this in two UBC Launch Pad projects that I worked on - Inertia and Pinpoint - and felt like it might be useful to have it as a standalone package.
Here’s a quick overview:
# Clientside
import "github.com/bobheadxi/res"
func main() {
resp, err := http.Get(os.Getenv("URL"))
if err != nil {
log.Fatal(err)
}
var info string
b, err := res.Unmarshal(resp.Body, res.KV{Key: "info", Value: &info})
if err != nil {
log.Fatal(err)
}
if err := b.Error(); err != nil {
log.Fatal(err)
}
println(info)
}
# Serverside
import "github.com/bobheadxi/res"
func Handler(w http.ResponseWriter, r *http.Request) {
res.R(w, r, res.MsgOK("hello world!",
"stuff", "amazing",
"details", res.M{"world": "hello"}))
}