aboutsummaryrefslogtreecommitdiff
path: root/reqres.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2020-10-27 12:21:51 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2020-10-27 12:21:51 +0100
commitd90eed44990f34a87c286ee21f5579506143040d (patch)
treedcae92b13e27e16c5d264caaaa89f9cd0f83a203 /reqres.go
parent214b8725f92c928383fd306f9074caf493ac6e93 (diff)
moved common json-write code into a helper
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
+}