aboutsummaryrefslogtreecommitdiff
path: root/reqres.go
diff options
context:
space:
mode:
Diffstat (limited to 'reqres.go')
-rw-r--r--reqres.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/reqres.go b/reqres.go
index d8d88f2..5d4c1bc 100644
--- a/reqres.go
+++ b/reqres.go
@@ -241,3 +241,17 @@ func UnpackJsonPost(r *http.Request, unpack interface{}) error {
}
return nil
}
+
+func WriteJsonResponse(response interface{}, w http.ResponseWriter) error {
+ json, err := json.Marshal(&response)
+ if err != nil {
+ return fmt.Errorf("json-encoding failed: %v", err)
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ _, err = w.Write(json)
+ if err != nil {
+ return fmt.Errorf("failed writing json response: %v", err)
+ }
+ return nil
+}