xml.go 612 B

12345678910111213141516171819202122232425262728293031
  1. package render
  2. import (
  3. "encoding/xml"
  4. "net/http"
  5. "github.com/pkg/errors"
  6. )
  7. // XML common xml struct.
  8. type XML struct {
  9. Code int
  10. Message string
  11. Data interface{}
  12. }
  13. var xmlContentType = []string{"application/xml; charset=utf-8"}
  14. // Render (XML) writes data with xml ContentType.
  15. func (r XML) Render(w http.ResponseWriter) (err error) {
  16. r.WriteContentType(w)
  17. if err = xml.NewEncoder(w).Encode(r.Data); err != nil {
  18. err = errors.WithStack(err)
  19. }
  20. return
  21. }
  22. // WriteContentType write xml ContentType.
  23. func (r XML) WriteContentType(w http.ResponseWriter) {
  24. writeContentType(w, xmlContentType)
  25. }