/
json.go
63 lines (53 loc) · 1.8 KB
/
json.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2017-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package util
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func RespondWithError(w http.ResponseWriter, code int, message string) {
RespondWithJSON(w, code, map[string]string{"error": message})
}
func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Strict-Transport-Security",
"max-age=63072000; includeSubDomains")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Referrer-Policy", "same-origin")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(response)
}
func RespondRaw(w http.ResponseWriter, code int, payload []byte) {
w.Header().Set("Strict-Transport-Security",
"max-age=63072000; includeSubDomains")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Referrer-Policy", "same-origin")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(payload)
}
// GetErrorFromJSON returns the error that is embedded in a JSON reply.
func GetErrorFromJSON(r io.Reader) (interface{}, error) {
var e interface{}
decoder := json.NewDecoder(r)
if err := decoder.Decode(&e); err != nil {
return nil, err
}
return e, nil
}
// FormatJSON returns a pretty printed JSON string for the provided structure.
func FormatJSON(v interface{}) string {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Sprintf("MarshalIndent: %v", err)
}
return string(b)
}