Skip to content

Instantly share code, notes, and snippets.

@mbn18
Created June 13, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbn18/a0cb320ea33068092d903495182c6526 to your computer and use it in GitHub Desktop.
Save mbn18/a0cb320ea33068092d903495182c6526 to your computer and use it in GitHub Desktop.
How to return google/jsonapi as JSON using echo
package main
import (
"github.com/google/jsonapi"
"github.com/labstack/echo"
"net/http"
)
type Album struct {
ID int `jsonapi:"primary,albums"`
Name string `jsonapi:"attr,name"`
}
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
jsonapi.MarshalManyPayload(c.Response(), albumList())
// Problem is that dat returned as Content-Type: text/plain
// Maybe force to content type to be JSON. Feel like not the proper way
return c.JSON(http.StatusOK, c.Response())
})
e.Logger.Fatal(e.Start(":1323"))
}
func albumList() []*Album {
a1 := Album{123, "allbum1"}
a2 := Album{456, "allbum2"}
albums := []*Album{&a1, &a2}
return albums
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment