Created
June 13, 2017 06:58
-
-
Save mbn18/a0cb320ea33068092d903495182c6526 to your computer and use it in GitHub Desktop.
How to return google/jsonapi as JSON using echo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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