summaryrefslogtreecommitdiff
path: root/app/internal/utils/json.go
blob: d3c6b717a4eb15070ca0db06b5a68beedd8fa78d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package utils

import (
	"encoding/json"
	"net/http"
)

func RespondWithError(w http.ResponseWriter, code int, message string) {
	RespondWithJSON(w, map[string]string{"error": message}, code)
}

func RespondWithJSON(w http.ResponseWriter, payload interface{}, code ...int) {
	var c int = http.StatusOK
	if len(code) > 0 {
		c = code[0]
	}
	response, _ := json.Marshal(payload)

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(c)
	w.Write(response)
}