/
json.go
36 lines (31 loc) · 944 Bytes
/
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
package utils
import (
"encoding/json"
"net/http"
"strconv"
)
//Costanti
const (
ContentType = "Content-Type"
ApplicationJSON = "application/json; charset=UTF-8"
)
//RespondWithError : funzione per la restituzione di un errore JSON
func RespondWithError(w http.ResponseWriter, code int, msg string) {
RespondWithJSON(w, code, map[string]string{
"status": strconv.Itoa(code),
"message": msg,
})
}
// RespondWithJSON : funzione per la restituzione di una risposta JSON
func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.MarshalIndent(payload, "", " ")
//response, _ := json.Marshal(payload)
w.Header().Set(ContentType, ApplicationJSON)
w.WriteHeader(code)
w.Write(response)
}
//ParsePost : funzione per il parsing di una richiesta POST
func ParsePost(request *http.Request, body interface{}) error {
decoder := json.NewDecoder(request.Body)
return decoder.Decode(&body)
}