Пример #1
0
func (r jsonApiRES) Apply(req *revel.Request, resp *revel.Response) {

	resp.WriteHeader(http.StatusOK, "application/vnd.api+json")

	if err := jsonapi.MarshalManyPayload(resp.Out, r); err != nil {
		http.Error(resp.Out, err.Error(), 500)
	}

}
Пример #2
0
func listBlogs(w http.ResponseWriter, r *http.Request) {
	// ... fetch your blogs and filter, offset, limit, etc ...

	blogs := testBlogsForList()

	w.WriteHeader(200)
	w.Header().Set("Content-Type", "application/vnd.api+json")
	if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
		http.Error(w, err.Error(), 500)
	}
}
Пример #3
0
func (h Employee) Index(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	emps := h.Repo.All()
	res := make([]interface{}, len(emps))
	for i := 0; i < len(emps); i++ {
		res[i] = &emps[i]
	}

	w.Header().Set("Content-Type", "application/vnd.api+json")
	w.WriteHeader(http.StatusOK)
	if err := jsonapi.MarshalManyPayload(w, res); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Пример #4
0
// encodeJSON is a helper function for returning objects in a JSON API spec format.
func encodeJSON(c *echo.Context, code int, v interface{}) error {
	c.Response().Header().Set("Content-Type", "application/vnd.api+json")
	c.Response().WriteHeader(code)

	s := reflect.ValueOf(v)
	if s.Kind() != reflect.Slice {
		return jsonapi.MarshalOnePayload(c.Response(), v)
	}

	ret := make([]interface{}, s.Len())
	for i := 0; i < s.Len(); i++ {
		ret[i] = s.Index(i).Interface()
	}
	return jsonapi.MarshalManyPayload(c.Response(), ret)
}