-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
52 lines (41 loc) · 1.03 KB
/
xml.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
package response
import (
"bytes"
"encoding/xml"
"net/http"
)
type xmlError struct {
xml.Name `xml:"Error"`
Code int `xml:"code"`
Message string `xml:"message"`
}
func XML(w http.ResponseWriter, status int, data any) {
buffer := new(bytes.Buffer)
err := xml.NewEncoder(buffer).Encode(data)
if err != nil {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to encode xml response."))
return
}
w.Header().Add("Content-Type", "application/xml")
w.WriteHeader(status)
w.Write(buffer.Bytes())
}
func XMLError(w http.ResponseWriter, status int, message string) {
payload := xmlError{
Code: status,
Message: message,
}
buffer := new(bytes.Buffer)
err := xml.NewEncoder(buffer).Encode(payload)
if err != nil {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(status)
w.Write([]byte("Failed to encode json response."))
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(buffer.Bytes())
}