github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/httputil/json.go (about) 1 package httputil 2 3 import ( 4 "encoding/json" 5 "net/http" 6 ) 7 8 // JSONEncode encodes the given object to json format and writes it to given ResponseWriter 9 func JSONEncode(rw http.ResponseWriter, v interface{}) error { 10 rw.Header().Set("Content-Type", "application/json") 11 return json.NewEncoder(rw).Encode(v) 12 } 13 14 // JSONEncodeWithCode encodes the given object to json format and writes it to given ResponseWriter 15 // with custom status code 16 func JSONEncodeWithCode(rw http.ResponseWriter, v interface{}, statusCode int) error { 17 rw.Header().Set("Content-Type", "application/json") 18 rw.WriteHeader(statusCode) 19 return json.NewEncoder(rw).Encode(v) 20 }