Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
560 B

package response
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// JSON makes a JSON response
func JSON(writer http.ResponseWriter, status int, data interface{}) {
jsonData, err := json.Marshal(data)
if err != nil {
log.Println("JSON Marshal failed: ", err.Error())
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
writer.WriteHeader(503)
fmt.Fprint(writer, "JSON marshalling failed:", err.Error())
return
}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(status)
writer.Write(jsonData)
}