diff options
author | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-10-27 12:21:51 +0100 |
---|---|---|
committer | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-10-27 12:21:51 +0100 |
commit | d90eed44990f34a87c286ee21f5579506143040d (patch) | |
tree | dcae92b13e27e16c5d264caaaa89f9cd0f83a203 /reqres.go | |
parent | 214b8725f92c928383fd306f9074caf493ac6e93 (diff) |
moved common json-write code into a helper
Diffstat (limited to 'reqres.go')
-rw-r--r-- | reqres.go | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -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 +} |