summary refs log tree commit diff
path: root/vendor/go.mau.fi/util/exhttp/json.go
blob: 48c8349df745d3c099a7e63947ed604136be6cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package exhttp

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

func WriteJSONResponse(w http.ResponseWriter, httpStatusCode int, jsonData any) {
	AddCORSHeaders(w)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(httpStatusCode)
	_ = json.NewEncoder(w).Encode(jsonData)
}

func WriteJSONData(w http.ResponseWriter, httpStatusCode int, data []byte) {
	AddCORSHeaders(w)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(httpStatusCode)
	_, _ = w.Write(data)
}

func WriteEmptyJSONResponse(w http.ResponseWriter, httpStatusCode int) {
	AddCORSHeaders(w)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(httpStatusCode)
	_, _ = w.Write([]byte("{}"))
}