Initial commit: Open sourcing all of the Maple Open Technologies code.
This commit is contained in:
commit
755d54a99d
2010 changed files with 448675 additions and 0 deletions
31
cloud/maplepress-backend/pkg/httpresponse/response.go
Normal file
31
cloud/maplepress-backend/pkg/httpresponse/response.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package httpresponse
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// JSON writes a JSON response with pretty printing (indented)
|
||||
func JSON(w http.ResponseWriter, code int, data interface{}) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
return encoder.Encode(data)
|
||||
}
|
||||
|
||||
// OK writes a 200 JSON response with pretty printing
|
||||
func OK(w http.ResponseWriter, data interface{}) error {
|
||||
return JSON(w, http.StatusOK, data)
|
||||
}
|
||||
|
||||
// Created writes a 201 JSON response with pretty printing
|
||||
func Created(w http.ResponseWriter, data interface{}) error {
|
||||
return JSON(w, http.StatusCreated, data)
|
||||
}
|
||||
|
||||
// NoContent writes a 204 No Content response
|
||||
func NoContent(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue