-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_base_xml.go
41 lines (36 loc) · 1.15 KB
/
response_base_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
package httpgo
import (
"encoding/xml"
"fmt"
"net/http"
)
// OKXMLResponse writes v into w with http.StatusOK.
func OKXMLResponse(w http.ResponseWriter, v any) error {
return XMLResponse(w, http.StatusOK, v)
}
func XMLResponse(w http.ResponseWriter, code int, v any) error {
return XMLResponseWithCode(w, code, wrapXmlBaseResponse(v))
}
// XMLResponseWithCode writes v as xml string into w with code.
func XMLResponseWithCode(w http.ResponseWriter, code int, v any) error {
return doWriteXml(w, code, v)
}
func doWriteXml(w http.ResponseWriter, code int, v any) error {
bs, err := xml.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return fmt.Errorf("marshal xml failed, error: %w", err)
}
w.Header().Set("Content-Type", ResponseContentTypeXML)
w.WriteHeader(code)
if n, err := w.Write(bs); err != nil {
// http.ErrHandlerTimeout has been handled by http.TimeoutHandler,
// so it's ignored here.
if err != http.ErrHandlerTimeout {
return fmt.Errorf("write response failed, error: %w", err)
}
} else if n < len(bs) {
return fmt.Errorf("actual bytes: %d, written bytes: %d", len(bs), n)
}
return nil
}