Skip to content

Commit

Permalink
Added HTTPAnswerJSON() to v1.1.4
Browse files Browse the repository at this point in the history
Signed-off-by: miguel <miguelpragier@gmail.com>
  • Loading branch information
miguelpragier committed May 8, 2020
1 parent 4dc19e8 commit 7a381aa
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions handyhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,20 @@ func HTTPJSONToStruct(r *http.Request, targetStruct interface{}, closeBody bool)

return err
}

// HTTPAnswerJSON converts the given data as json, set the content-type header and write it to requester
func HTTPAnswerJSON(w http.ResponseWriter, data interface{}) error {
jb, err := json.Marshal(data)

if err != nil {
return err
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")

if _, errw := w.Write(jb); errw != nil {
return errw
}

return nil
}

0 comments on commit 7a381aa

Please sign in to comment.