dex/vendor/github.com/mbanzon/simplehttp/json_utils.go

32 lines
619 B
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package simplehttp
import (
"encoding/json"
"net/http"
)
func GetJSONInput(r *http.Request, w http.ResponseWriter, v interface{}) (err error) {
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(v)
if err != nil {
http.Error(w, "Bad request.", http.StatusBadRequest)
return err
}
return nil
}
func OutputJSON(w http.ResponseWriter, v interface{}) (err error) {
var data []byte
data, err = json.Marshal(v)
if err != nil {
http.Error(w, "Internal error.", http.StatusInternalServerError)
return err
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(data)
return nil
}